468,507 Members | 1,662 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,507 developers. It's quick & easy.

Moving pivot locations using Trigonometry

Hi

I have a math kinda problem where I'm trying to split some lines when two or
more lines connect two shapes. The reason I am doing this is to make it
clear that there are multiple lines connecting the two shapes.

http://www.blackwaterbadgers.co.uk/image1.bmp

Image 1 shows how if you have a line between two shapes, the line is
straight. if you have more than one line, the lines will start overlapping
each other and essentially look like just the one line. What I have done is
to add a pivot to the centre of the line so the lines can be given a curve
as shown in Image 1.

The middle pivot is created when the line is straight so I know its
location. I also know the locations of either end of the line so from this
I can get the angle of the line. I also know the distance by which I want
to offset the middle pivot.

My plan is to run some code (most of this I have written) that looks to see
how many lines are going between the two shapes. If it is one line then I
will leave it alone. If it is more than one line, I want to separate out
the lines.

http://www.blackwaterbadgers.co.uk/image2.bmp

Based on Image 2
I know the location of the two red dots which give me oppA and adjA, from
this I can get the angle x. As I want the middle pivot to move at a right
angle to its origin, I know the angle of y.
hypB is the distance I want to move the pivot from its origin so from this I
can get oppB and adjB.

Based on the middle pivot, I can now use these measurements to offset the
middle pivot.

The only problem is, because I have used Math.Abs to ensure that I now
working with negative numbers, I now need to establish the direction in
which I want to offset the pivot. This isn't too much of a problem but I am
having trouble working out the distance by which to offset the pivot based
on the number of lines.

So my question is, how can I determine the offset direct and length based on
the number of lines?

Ideally, I would like to split the lines evenly so that if I had an odd
number of lines then the middle line would stay straight with the remaining
lines either side. If I have an even number of lines then the lines would
be split evenly.

A colleague suggested going through each line and offsetting each pivot in
the same direction and then moving all the pivots back to a more central
position but I'm sure it would be better to just work out the direction to
move each pivot and by how much.

Does anyone have any idea on how I could achieve this?

Regards, Carl Gilbert
Nov 21 '05 #1
2 2779
Sounds to me like a job for beziers...

You'll find a little VB application after my signature.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Imports System.Drawing.Drawing2D

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents NumericUpDown1 As System.Windows.Forms.NumericUpDown

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents Label2 As System.Windows.Forms.Label

Friend WithEvents NumericUpDown2 As System.Windows.Forms.NumericUpDown

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.NumericUpDown1 = New System.Windows.Forms.NumericUpDown

Me.Label1 = New System.Windows.Forms.Label

Me.Label2 = New System.Windows.Forms.Label

Me.NumericUpDown2 = New System.Windows.Forms.NumericUpDown

CType(Me.NumericUpDown1,
System.ComponentModel.ISupportInitialize).BeginIni t()

CType(Me.NumericUpDown2,
System.ComponentModel.ISupportInitialize).BeginIni t()

Me.SuspendLayout()

'

'NumericUpDown1

'

Me.NumericUpDown1.Location = New System.Drawing.Point(72, 16)

Me.NumericUpDown1.Maximum = New Decimal(New Integer() {10, 0, 0, 0})

Me.NumericUpDown1.Minimum = New Decimal(New Integer() {1, 0, 0, 0})

Me.NumericUpDown1.Name = "NumericUpDown1"

Me.NumericUpDown1.Size = New System.Drawing.Size(72, 20)

Me.NumericUpDown1.TabIndex = 0

Me.NumericUpDown1.Value = New Decimal(New Integer() {3, 0, 0, 0})

'

'Label1

'

Me.Label1.Location = New System.Drawing.Point(8, 16)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(40, 23)

Me.Label1.TabIndex = 1

Me.Label1.Text = "Lines"

'

'Label2

'

Me.Label2.Location = New System.Drawing.Point(8, 48)

Me.Label2.Name = "Label2"

Me.Label2.Size = New System.Drawing.Size(56, 23)

Me.Label2.TabIndex = 2

Me.Label2.Text = "Degrees"

'

'NumericUpDown2

'

Me.NumericUpDown2.Location = New System.Drawing.Point(72, 48)

Me.NumericUpDown2.Maximum = New Decimal(New Integer() {90, 0, 0, 0})

Me.NumericUpDown2.Minimum = New Decimal(New Integer() {1, 0, 0, 0})

Me.NumericUpDown2.Name = "NumericUpDown2"

Me.NumericUpDown2.Size = New System.Drawing.Size(72, 20)

Me.NumericUpDown2.TabIndex = 0

Me.NumericUpDown2.Value = New Decimal(New Integer() {1, 0, 0, 0})

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(448, 382)

Me.Controls.Add(Me.Label2)

Me.Controls.Add(Me.Label1)

Me.Controls.Add(Me.NumericUpDown1)

Me.Controls.Add(Me.NumericUpDown2)

Me.Name = "Form1"

Me.Text = "Form1"

CType(Me.NumericUpDown1, System.ComponentModel.ISupportInitialize).EndInit( )

CType(Me.NumericUpDown2, System.ComponentModel.ISupportInitialize).EndInit( )

Me.ResumeLayout(False)

End Sub

#End Region

Protected Overrides Sub OnMouseMove(ByVal e As
System.Windows.Forms.MouseEventArgs)

mouse = New Point(e.X, e.Y)

Invalidate()

End Sub

Dim mouse As Point

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)

Dim n As Integer

Dim absoluteangle As Single = 0

For n = 0 To Me.NumericUpDown1.Value - 1

Dim rotationangle As Single

If (n Mod 2 = 0) Then

rotationangle = absoluteangle

Else

rotationangle = -absoluteangle

End If

Dim firstPoint As PointF = New PointF(Me.ClientRectangle.Width / 2, 20)

Dim lastpoint As PointF = New PointF(mouse.X, mouse.Y)

Dim vector As PointF = New PointF(lastpoint.X - firstPoint.X, lastpoint.Y -
firstPoint.Y)

'get the hypotenuse...

Dim hypot As Single = CSng(Math.Sqrt((vector.X * vector.X) + (vector.Y *
vector.Y)))

'create a point at the center of the hypotenuse...

Dim center As New PointF(firstPoint.X + (vector.X / 2), firstPoint.Y +
(vector.Y / 2))

'Create bezier control points

Dim cp1 As PointF() = New PointF() {center}

Dim cp2 As PointF() = New PointF() {center}

'rotate one point around the first position...

Dim mx As New Matrix

mx.RotateAt(rotationangle, firstPoint)

mx.TransformPoints(cp1)

'rotate the other point around the last position...

mx = New Matrix

mx.RotateAt(-rotationangle, lastpoint)

mx.TransformPoints(cp2)

'draw a bezier

e.Graphics.DrawBezier(Pens.Black, firstPoint, cp1(0), cp2(0), lastpoint)

absoluteangle += Me.NumericUpDown2.Value

Next

End Sub

End Class

"Carl Gilbert" <cn*@newsgroup.nospam> wrote in message
news:uz*************@TK2MSFTNGP10.phx.gbl...
Hi

I have a math kinda problem where I'm trying to split some lines when two
or more lines connect two shapes. The reason I am doing this is to make
it clear that there are multiple lines connecting the two shapes.

http://www.blackwaterbadgers.co.uk/image1.bmp

Image 1 shows how if you have a line between two shapes, the line is
straight. if you have more than one line, the lines will start
overlapping each other and essentially look like just the one line. What
I have done is to add a pivot to the centre of the line so the lines can
be given a curve as shown in Image 1.

The middle pivot is created when the line is straight so I know its
location. I also know the locations of either end of the line so from
this I can get the angle of the line. I also know the distance by which I
want to offset the middle pivot.

My plan is to run some code (most of this I have written) that looks to
see how many lines are going between the two shapes. If it is one line
then I will leave it alone. If it is more than one line, I want to
separate out the lines.

http://www.blackwaterbadgers.co.uk/image2.bmp

Based on Image 2
I know the location of the two red dots which give me oppA and adjA, from
this I can get the angle x. As I want the middle pivot to move at a right
angle to its origin, I know the angle of y.
hypB is the distance I want to move the pivot from its origin so from this
I can get oppB and adjB.

Based on the middle pivot, I can now use these measurements to offset the
middle pivot.

The only problem is, because I have used Math.Abs to ensure that I now
working with negative numbers, I now need to establish the direction in
which I want to offset the pivot. This isn't too much of a problem but I
am having trouble working out the distance by which to offset the pivot
based on the number of lines.

So my question is, how can I determine the offset direct and length based
on the number of lines?

Ideally, I would like to split the lines evenly so that if I had an odd
number of lines then the middle line would stay straight with the
remaining lines either side. If I have an even number of lines then the
lines would be split evenly.

A colleague suggested going through each line and offsetting each pivot in
the same direction and then moving all the pivots back to a more central
position but I'm sure it would be better to just work out the direction to
move each pivot and by how much.

Does anyone have any idea on how I could achieve this?

Regards, Carl Gilbert

Nov 21 '05 #2
Thanks Bob

Your code worked just fine with a bit of tweaking.

Regards, Carl

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Prasad Patil | last post: by
2 posts views Thread by Rob | last post: by
3 posts views Thread by Jerry K via DotNetMonster.com | last post: by
1 post views Thread by kingster | last post: by
3 posts views Thread by nikila | last post: by
9 posts views Thread by PeteCresswell | last post: by
reply views Thread by linkswanted | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.