Drawing a line from x,y to xx,yy

EFileTahi-A

Well-known member
Joined
Aug 8, 2004
Messages
539
Location
Portugal / Barreiro
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, lets 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...

Code:
//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... :)
 
Last edited by a moderator:
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.

[VB]
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
[/code]

Those might help.
 
Back
Top