473,503 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3093
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2073
by: Prasad Patil | last post by:
Hi, I have created a report in excel, it uses Pivot tables. The excel has two sheets 1: Pivot ( Contains the Pivot Table) 2: Data (Data Requred to populate the pivot table I create an...
2
5794
by: Rob | last post by:
I'm just getting around to using pivot tables and charts. I find the Pivot table interface to be INCREDIBLY frustrating. When I view a table in Design view, then choose Pivot table view, I get...
1
3128
by: Grey | last post by:
I have created a asp.net form for user to input data. After input the data, user need to click a button to export the input data to excel for data analysis with excel pivot table function. is it...
3
10241
by: Jerry K via DotNetMonster.com | last post by:
I'm creating a pivot table using vb.net and the data is from sqlserver (desktop). I have been successful at creating the pivot table, which includes a 'date' column field. I'd like to group the...
1
7330
by: kingster | last post by:
Hi, I have a regular dataset and all i want to do is make a pivot table display in a browser with the datasource of the pivot table to be this dataset and then the end-user will be able to do...
3
8476
by: nikila | last post by:
Hi, I have to create excel pivot tables from vb.net. Already I am creating excel file using oledb connection. I want to use the same to create the excel pivot tables. Can anyone please help me...
9
6771
by: PeteCresswell | last post by:
I've got something called "Reference Rates". The idea is that on a given day, we have various rates of return for various entities. e.g. Libor 3-month return, Libor 6-month return, US Treasury...
0
1443
by: linkswanted | last post by:
World Moving & Storage Moving Companies and Movers We would like to take this opportunity to introduce "World Moving & Storage" to You. "World Moving & Storage" is well established, Fully...
1
1907
by: STUFIX | last post by:
Hi all, I created a pivot table that allowed users to select certain customers from a drop down list and display the relevant data - it worked fine but has now stopped allowing them to do that. ...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7274
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.