Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

How can I draw a line from one point to another?

 

Imagine that the starting point will be 100,100(x,y(pixels)) to the mouse click coordenates, let's say 345,201 and a line will be drawn from these two points. I can do this all right, but the problem is to make it animated, like seeing the line being drawn from x,y to xx,yy using a timer, so that a every tick will increase the lines size from the starting point to the target point...

 


//each line represents a tick... (frame per second)
//x,y means the starting point
//xx,yy means the ending point
// "----" means the line to be drawn

x,y  -                    xx,yy 
x,y  --                   xx,yy
x,y  ---                  xx,yy
x,y  ----                 xx,yy
x,y   ----                xx,yy 
x,y    ----               xx,yy 
x,y     ----              xx,yy 
x,y      ----             xx,yy
x,y       ----            xx,yy   
x,y        ----           xx,yy   
x,y         ----          xx,yy   
x,y          ----         xx,yy
x,y           ----        xx,yy    
x,y            ----       xx,yy     
x,y             ----      xx,yy  
x,y              ----     xx,yy  
x,y               ----    xx,yy    
x,y                ----   xx,yy  
x,y                 ----  xx,yy  
x,y                  ---- xx,yy 
x,y                   --- xx,yy    
x,y                    -- xx,yy  
x,y                     - xx,yy  
x,y                       xx,yy                                                 

 

If using a line seems to be too tricky, at least I would like to know how to animate a "dot" from the too points...

 

I guess this will involve Math.Cos / Match.P / Match.Atan / Math.Sin...

Unfortunately my math skills stink...

 

If there is someone kind enough to teach me this, please... :)

Edited by EFileTahi-A
  • Leaders
Posted

You are... looking for an algorithm to acheive the effect of the line slowly being drawn from point a to point b?

 

If so, why not make a function to give you intermediate points within the line so that you may draw the line one portion at a time using a timer. Use a simple weighted average function to get the intermediate points, and draw the segments on a timer.

 

   Function IntermediatePoints(ByVal Point1 As Point, ByVal Point2 As Point, ByVal Segments As Integer) As Point()
       Dim Result As Point() = New Point(Segments) {}
       Dim StepSize As Double = 1 / Segments
       Dim StepVal As Double
       Result(0) = Point1
       Result(Segments) = Point2
       For i As Integer = 1 To Segments - 1
           StepVal += StepSize
           Result(i) = PointAverage(Point1, Point2, StepVal)
       Next
       Return Result
   End Function

   Function PointAverage(ByVal Point1 As Point, ByVal Point2 As Point, ByVal Weight As Double) As Point
       Dim Result As Point
       Result.X = CInt(Point1.X * Weight + Point2.X * (1 - Weight))
       Result.Y = CInt(Point1.Y * Weight + Point2.Y * (1 - Weight))
       Return Result
   End Function

 

Those might help.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...