Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Get DiagramLink coordinates halfway in the link (Read 1797 times)
Bass
YaBB Newbies
*
Offline



Posts: 31
Joined: Aug 27th, 2010
Get DiagramLink coordinates halfway in the link
Jun 15th, 2011 at 10:05am
Print Post  
Hi,

I would like to programmatically create a ControlNode halfway down a diagram link, but am not sure how to approach this.

During diagram design, the user can drag and drop a ControlNode on a link, and this will split the link into two parts allowing the ControlNode to fit nicely in the middle.

Now I want to the same thing pogrammatically, when the diagram is loaded and it is determined that a ControlNode exists for a particular link.

The problem is that I want to create the ControlNode in the middle of the link. What would be a good way to get a PointF object based on the coordinates of the middle of the diagram link?

The only alternative I see would be to save the coordinates with the ControlNode back to the database.. but I would like to avoid that if possible.

Regards,
Bas


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


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Get DiagramLink coordinates halfway in the lin
Reply #1 - Jun 15th, 2011 at 10:51am
Print Post  
Hi,

Assuming a multi-segment Polyline or Cascading link, you can find the point at its exact middle using this method with 0.5 as argument:

Code
Select All
PointF PointAlongLength(DiagramLink link, float lengthFactor)
{
	float lengthPos = lengthFactor * link.Length;
	float totalLength = 0;
	var segments = link.ControlPoints;

	for (int i = 0; i < segments.Count - 1; ++i)
	{
		PointF point1 = segments[i];
		PointF point2 = segments[i + 1];
		float segmentLength = Utilities.Distance(point1, point2);
		if (totalLength + segmentLength > lengthPos)
		{
			// the wanted point is in current segment
			float normalX = (point2.X - point1.X) / segmentLength;
			float normalY = (point2.Y - point1.Y) / segmentLength;
			float pointOffset = lengthPos - totalLength;
			return new PointF(
				point1.X + normalX * pointOffset,
				point1.Y + normalY * pointOffset);
		}
		totalLength += segmentLength;
	}
	return segments[0];
} 



If you need to find that for Bezier links, you will have to approximate them through straight-line segments.

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



Posts: 31
Joined: Aug 27th, 2010
Re: Get DiagramLink coordinates halfway in the lin
Reply #2 - Jun 15th, 2011 at 11:53am
Print Post  
Thanks for quick and working answer. A+ for support here.

Regards,
Bas
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint