c# - Move a rectangle using angles -
i need move rectangle using angles. want change direction of moving rectangle when reaches location have given in code in if statement!
i need way can find out how move rectangle @ 60, 30, 60, 120, 150, 270 degrees!
suppose if
circle.y>=this.height-80
see this:
i need change direction of rectangle movement using angles! @ location reaches can change rectangle direction according angle of own choice! such that:
if(circle.y>=this.height-80) move in direction of 90 degrees if(circle.x>=this.width-80) move in direction of 60 degree
as can see in screen shot!
what have been trying is:
public partial class form1 : form { rectangle circle; double dx = 2; double dy = 2; public form1() { initializecomponent(); circle = new rectangle(10, 10, 40, 40); } private void form1_load(object sender, eventargs e) { this.refresh(); } private void form1_paint(object sender, painteventargs e) { graphics g = e.graphics; g.smoothingmode = smoothingmode.antialias; g.fillellipse(new solidbrush(color.red), circle); } private void timer_tick(object sender, eventargs e) { circle.x += (int)dx; circle.y += (int)dy; if (circle.y>=this.height-80) { dy = -math.acos(0) * dy/dy; //here want change direction of circle @ 90 degrees should go vertically straight same speed } this.refresh(); } }
the problem have been trying changing conditions to:
dy = -math.asin(1) * dy; dx = math.acos(0) * dx ;
but in both cases nothing happening , direction remains same! want move circle in inverted upward direction @ 90 degrees when reach @
circle.y>=this.height-80
you need draw rectangle again image display. created code moving , drawing rectangle on picturebox1
, using defined circle
-rectangle:
moving rectangle:
public void moverectangle(ref rectangle rectangle, double angle, double distance) { double angleradians = (math.pi * (angle) / 180.0); rectangle.x = (int)((double)rectangle.x - (math.cos(angleradians) * distance)); rectangle.y = (int)((double)rectangle.y - (math.sin(angleradians) * distance)); }
drawing rectangle , displaying in picturebox
:
public void drawrectangle(rectangle rectangle) { bitmap bmp = new bitmap(picturebox1.width, picturebox1.height); using (graphics g = graphics.fromimage(bmp)) { g.smoothingmode = system.drawing.drawing2d.smoothingmode.antialias; g.fillellipse(new solidbrush(color.red), rectangle); } picturebox1.image = bmp; }
demo button click:
private void button1_click(object sender, eventargs e) { moverectangle(ref circle, 90, 5); drawrectangle(circle); }
Comments
Post a Comment