Jump to content
  • Advertisement
Sign in to follow this  
garusher

Vector

This topic is 2591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi

I am wanting to create a rectangle for two points (vector). I need a function that takes two points and a length and calculates a perpendicular point from line (vector) AB, to a given length.

I found this code, however it doesn't seem to work.


Private Function doit(ByVal a As PointF, ByVal b As PointF, ByVal l As Double) As PointF Dim lfraction, dx, dy As Double dx = b.X - a.X dy = b.Y - a.Y If dx = 0 And dy = 0 Then Exit Function lfraction = l / Math.Sqrt(dx * dx + dy * dy) Return New Point(b.X + lfraction * dy, b.Y + lfraction * dx) End Function
Is there any easy or other methods i can use to create a rectangle from a line

Maybe a line stroke method or something.

Thanks

Gary

Share this post


Link to post
Share on other sites
Advertisement
I am not sure I understand what you are saying, so let me know if this doesn't answer your question.

If you have a vector (x,y), the vectors (y,-x) and (-y,x) are perpendicular to it (you have a choice as to whether you want to rotate 90 degrees one way or the other). If you want a vector of a particular length, you can divide by the length of the vector and multiply by the desired length.

If you now have points (Ax,Ay) and (Bx,By), you can compute a point (Cx,Cy) such that the vectors (Bx-Ax,By-Ay) and (Cx-Ax,Cy-Ay) are perpendicular and (Cx-Ax,Cy-Ay) has the desired length:
Vx = By-Ay
Vy = -(Bx-Ax)
initial_length = sqrt(Vx*Vx+Vy*Vy)
scale = desired_length/initial_length
scaled_Vx = Vx*scale
scaled_Vy = Vy*scale
Cx = Ax + scaled_Vx
Cy = By + scaled_Vy

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!