By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,569 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,569 IT Pros & Developers. It's quick & easy.

[Maths] Drawing lines to shapes

P: 28
Hi,

I'm making a brainstorming program in VB.net and am struggling with drawing arrow lines. Currently I have lines that go all the way to the centre of a 'bubble' on the screen. This works very well but I want to implement lines with arrows to show flow.

I need the lines to stop just before it gets to the bounds of the bubble otherwise the arrow head will be drawn under the shape.

I've been trying for hours to get this to work. I've tried Pythagoras and sin cos tan but nothing seems to work well.

The current code I have isn't working:
Expand|Select|Wrap|Line Numbers
  1.    If (lastPoint.X < firstPoint.X) Then
  2.                     If (lastPoint.Y < firstPoint.Y) Then
  3.                         '' bottom left
  4.                         Dim x As Double = Math.Atan((firstPoint.X - lastPoint.X) / (firstPoint.Y - lastPoint.Y))
  5.                         Dim s2 As Double = Math.Tan(x) * (endSize.Height / 2)
  6.  
  7.                         Dim newX As Double = lastPoint.X + s2
  8.                         Dim newY As Double = lastPoint.Y
  9.                         alteredLastPoint = New Point(newX, newY)
  10.                     Else
  11.                         '' top left
  12.                         Dim x As Double = Math.Atan((firstPoint.X - lastPoint.X) / (firstPoint.Y - lastPoint.Y))
  13.                         Dim s2 As Double = Math.Tan(x) * (endSize.Height / 2)
  14.  
  15.                         Dim newX As Double = lastPoint.X + s2
  16.                         Dim newY As Double = lastPoint.Y + endSize.Height
  17.                         alteredLastPoint = New Point(newX, newY)
  18.                     End If
  19.                 Else
  20.                     If (lastPoint.Y < firstPoint.Y) Then
  21.                         '' bottom right
  22.                         Dim x As Double = Math.Atan((firstPoint.X - lastPoint.X) / (firstPoint.Y - lastPoint.Y))
  23.                         Dim s2 As Double = Math.Tan(x) * (endSize.Height / 2)
  24.  
  25.                         Dim newX As Double = lastPoint.X + s2
  26.                         Dim newY As Double = lastPoint.Y
  27.                         alteredLastPoint = New Point(newX, newY)
  28.                     Else
  29.                         '' top right
  30.                         Dim x As Double = Math.Atan((firstPoint.X - lastPoint.X) / (firstPoint.Y - lastPoint.Y))
  31.                         Dim s2 As Double = Math.Tan(x) * (endSize.Height / 2)
  32.  
  33.                         Dim newX As Double = lastPoint.X + s2
  34.                         Dim newY As Double = lastPoint.Y + endSize.Height
  35.                         alteredLastPoint = New Point(newX, newY)
  36.                     End If
  37.                 End If
It vaguely works but is by far not perfect. I am quite new to this kind of maths so it's probably just something simple that I'm forgetting.

alteredLastPoint is the point for the where the arrow head will be drawn.
lastPoint is the centre point of the bubble where the arrow head is meant to point to
firstPoint is the centre point of the bubble where the arrow will come from
endSize is the size of the bubble where the arrow points to.

Thanks in advance,
Josh.
Feb 2 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.