Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Silverlight - Position Elements

Status
Not open for further replies.

kidfug

Programmer
Mar 7, 2006
7
US
I'm having a problem positioning elements and not sure where I'm going wrong. The elements are basically ellipsegeometry paths, since all of the elements are have the same radius.x and radius.y I've added a property Radius that I set when I add them.

This code should position them without an overlap and it works fine as long as the elements have the same radius



1 Public Sub Group2Elements(ByVal Conductor1 As Conductor, ByVal Conductor2 As Conductor, ByVal Angle As Double)
2 Dim DeltaX As Double = System.Math.Cos(Radians(Angle)) * (Conductor1.Radius + Conductor2.Radius)
3 Dim DeltaY As Double = System.Math.Sin(Radians(Angle)) * (Conductor1.Radius + Conductor2.Radius)
4 Dim Cond2Center As Point = New Point(Conductor2.GetValue(Canvas.LeftProperty) + Conductor2.Radius, Conductor2.GetValue(Canvas.TopProperty) + Conductor2.Radius)
5 Conductor1.SetValue(Canvas.LeftProperty, (Cond2Center.X + DeltaX) - Conductor1.Radius)
6 Conductor1.SetValue(Canvas.TopProperty, (Cond2Center.Y + DeltaY) - Conductor1.Radius)
7 End Sub

and the function to convert Degrees to Radians is as follows



1 Private Function Radians(ByVal Deg As Double) As Double
2 Radians = Deg * System.Math.PI / 180
3 End Function

The other problem I'm having is positioning 1 element next to 2 other elements, once again if the elements have the same radius it seems to work.

1 Public Sub Group3Elements(ByVal Conductor1 As Conductor, ByVal Conductor2 As Conductor, ByVal Conductor3 As Conductor, ByVal Side As Integer)
2 Dim DeltaX As Double
3 Dim DeltaY As Double
4 Dim Angle As Double
5 Dim aLength, bLength, cLength As Double
6 Dim aAngle, bAngle, cAngle As Double
7 aLength = Conductor3.Radius + Conductor1.Radius
8 bLength = Conductor2.Radius + Conductor1.Radius
9 cLength = Conductor2.Radius + Conductor3.Radius
10 aAngle = Degrees(System.Math.Acos((bLength ^ 2 + cLength ^ 2 - aLength ^ 2) / (2 * bLength * cLength)))
11 bAngle = Degrees(System.Math.Acos((aLength ^ 2 + cLength ^ 2 - bLength ^ 2) / (2 * aLength * cLength)))
12 cAngle = Degrees(System.Math.Acos((aLength ^ 2 + bLength ^ 2 - cLength ^ 2) / (2 * aLength * bLength)))
13 If Side = 0 Then
14 Angle = AngleBetween(Conductor2, Conductor3) - aAngle
15 Else
16 Angle = AngleBetween(Conductor2, Conductor3) + aAngle
17 End If
18 DeltaX = System.Math.Cos(Radians(Angle)) * (Conductor2.Radius + Conductor1.Radius)
19 DeltaY = System.Math.Sin(Radians(Angle)) * (Conductor2.Radius + Conductor1.Radius)
20 Dim Cond2Center As Point = New Point(Conductor2.GetValue(Canvas.LeftProperty) + Conductor2.Radius, Conductor2.GetValue(Canvas.TopProperty) + Conductor2.Radius)
21 Conductor1.SetValue(Canvas.LeftProperty, (Cond2Center.X + DeltaX) - Conductor1.Radius)
22 Conductor1.SetValue(Canvas.TopProperty, (Cond2Center.Y + DeltaY) - Conductor1.Radius)
23 End Sub
24

and the AngleBeteween code is as follows



Public Function AngleBetween(ByVal Conductor1 As Conductor, ByVal Conductor2 As Conductor) As Double
Dim Angle As Double
Dim Cond1Center As Point = New Point(Conductor1.GetValue(Canvas.LeftProperty) + Conductor1.Radius, Conductor1.GetValue(Canvas.TopProperty) + Conductor1.Radius)
Dim Cond2Center As Point = New Point(Conductor2.GetValue(Canvas.LeftProperty) + Conductor2.Radius, Conductor2.GetValue(Canvas.TopProperty) + Conductor2.Radius)
Angle = Degrees(System.Math.Asin((Cond2Center.Y - Cond1Center.Y) / (Conductor2.Radius + Conductor1.Radius)))
If Cond2Center.X < Cond1Center.X Then Angle = 180 - Angle
AngleBetween = Angle
End Function

and finally the the function to convert Degrees to Radians is

1 Private Function Degrees(ByVal rad As Double) As Double
2 Degrees = rad * 180 / System.Math.PI
3 End Function

Again both Group2Elements & Group3Elements work if the elements all have the same radius. All the ellipses in the end need to be nested without overlapping.

Any help would be appreciated

Thanks



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top