[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1285
  • Last Modified:

How to make a picture box move along a line.

I have a picture box  pict and two point A and B.
I want picture box move on the line connect two point A and B.
How to make this?
Thanks
0
colorsit123
Asked:
colorsit123
3 Solutions
 
ashutosh_kumarCommented:
use the location property of the picture box and set the cordinates...

now to get the coordinates on the line joining A & B, you need to use little line formula and find out the coordinates....

I can post that too, if you want...but hope you know it if you have studied geometry in school days!! (no offense here!!!)
0
 
JimBrandleyCommented:
After you have the PictureBox displayed, and want to start the motion, create and start a timer.

// Add these Class members
private Point startLocation = new Point( 0, 0); // Initialize to desired start
private Point endLocation = new Point( 10, 10); // Initialize to desired end
private int stepCount = 10;                     // Initialize to desired number of steps
private int steps = 0;                          // This is the number of times we have moved the control
 
This is the event handler for the timer:
private void timer_Tick(object sender, EventArgs e)
{
   steps += 1;
   if (steps >= stepCount) // Done
   {
      // yourTimer.Dispose();
      return;
   }
   double ratio = (double)steps / (double)stepCount;
   int newX = startLocation.X + (int)((double)(endLocation.X - startLocation.X) * ratio);
   int newY = startLocation.Y + (int)((double)(endLocation.Y - startLocation.Y) * ratio);
   Point current = new Point(newX, newY);
   myPictureBox.Location = current;
}

Jim
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You need to make a design decision here...

Do you want:

(1) A fixed number of "jumps" between the points?  (this is what JimBrandley has done) Your picturebox would always take the same amount of time to travel the distance between "A" and "B" irregardless of the distance between them...whether it be 50 pixels or 1000 pixels.

(2) A fixed rate of travel between the points?  Here you would need to calculate the amount of time it would take to travel the distance and then either adjust your timer accordingly (for a fixed number of jumps), or calculate the "percentage" or the time currently transpired and determine the location that corresponds to that "time".
0
 
colorsit123Author Commented:
Great!
Thanks ashutosh_kumar for the Idea
Thanks Jim for the code
And thanks Idle_Mind for the fixed rate of travel.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now