Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Attaching Node To Rotated Node (Read 1400 times)
FrankR
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 4
Joined: Jun 7th, 2010
Attaching Node To Rotated Node
Jun 7th, 2010 at 7:04pm
Print Post  
I want to programmatically attach a 2nd SVGNode to a specific side of an existing SVGNode in a Diagram so their sides line up. The user drags an icon from a list and drops it onto the Diagram and I calculate the position to insert the 2nd node based on the existing target node the user drops the 2nd node on.

I can do this using the AttachTo method when the target node has not been rotated but cannot accurately calculate the 2nd node's correct insertion location when the target node has been rotated. I am using the target node's GetRotatedBounds method but still have to calculate the offset for the 2nd node based on its size, especially when trying to line the 2nd node up with the target node's left side.

I'm also accurately getting the 2nd node's rotation angle by setting it equal to the target node's GetRotationAngle value.

Any suggestions?

Thanks

Frank
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Attaching Node To Rotated Node
Reply #1 - Jun 8th, 2010 at 5:59am
Print Post  
This method seems to do the trick:

Code
Select All
private void AlignRotatedNodes(
	ShapeNode anchorNode, float nX, float nY,
	ShapeNode attachNode, float tX, float tY)
{
	// the absolute coordinates of the anchor node alignment point
	RectangleF anchorRect = anchorNode.Bounds;
	Matrix t = new Matrix();
	PointF c = new PointF(
		anchorRect.X + anchorRect.Width / 2,
		anchorRect.Y + anchorRect.Height / 2);
	t.RotateAt(anchorNode.RotationAngle, c);
	PointF[] n = new PointF[] { new PointF(
		anchorRect.X + nX * anchorRect.Width,
		anchorRect.Y + nY * anchorRect.Height) };
	t.TransformPoints(n);
	float nx = n[0].X;
	float ny = n[0].Y;

	// the absolute coordinates of the attached node alignment point
	RectangleF attachRect = attachNode.Bounds;
	float tx = attachRect.X + tX * attachRect.Width;
	float ty = attachRect.Y + tY * attachRect.Height;

	// this transform moves and rotates the attached node to align the specified points
	Matrix m = new Matrix();
	m.RotateAt(anchorNode.RotationAngle, new PointF(tx, ty));
	m.Translate(nx - tx, ny - ty, MatrixOrder.Append);

	// find the new position of the attached node center
	float cx = attachRect.X + attachRect.Width / 2;
	float cy = attachRect.Y + attachRect.Height / 2;
	PointF[] newCenterT = new PointF[] { new PointF(cx, cy) };
	m.TransformPoints(newCenterT);

	// move the node
	float newX = newCenterT[0].X - attachRect.Width / 2;
	float newY = newCenterT[0].Y - attachRect.Height / 2;
	attachNode.Move(newX, newY);
	attachNode.RotationAngle = anchorNode.RotationAngle;
}

ShapeNode n1 = diagram.Factory.CreateShapeNode(50, 50, 30, 40);
n1.RotationAngle = 120;
ShapeNode n2 = diagram.Factory.CreateShapeNode(130, 60, 20, 30);
AlignRotatedNodes(n1, 0, 0.5f, n2, 1, 0.5f); 



The float arguments specify relative point coordinates for the node points that should be aligned, expressed as fraction of the nodes' width and height. If you already know the points absolute coordinates, you can skip the part of the method that calculates nx,ny and tx,ty and pass them as arguments instead.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
FrankR
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 4
Joined: Jun 7th, 2010
Re: Attaching Node To Rotated Node
Reply #2 - Jun 8th, 2010 at 4:55pm
Print Post  
That works wonderfully.

Thanks.

FrankR
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint