472,780 Members | 1,200 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

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 3018
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
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
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
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
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
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
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
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
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
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
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.