Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Understanding the result from ShapeNode.GetIntersection (Read 4427 times)
mihai
Junior Member
**
Offline



Posts: 86
Joined: Jul 29th, 2009
Understanding the result from ShapeNode.GetIntersection
Sep 10th, 2014 at 12:49pm
Print Post  
Hi,

I have a rectangle ShapeNode with the bounds:
Code
Select All
left: 30
top: 53
right: 55
bottom: 827.67932489451482 



and I need to find out the intersection points with the horizontal line:
Code
Select All
BeginPoint	{6.99999999999999; -24.4679324894515}
EndPoint	{282; -24.4679324894515} 



However, the result from ShapeNode.GetIntersection is:
Code
Select All
{12.5; 387.339662447257} 



What am I missing?

Thanks,
-Mihai
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Understanding the result from ShapeNode.GetIntersection
Reply #1 - Sep 10th, 2014 at 1:29pm
Print Post  
Hi,

This method wasn't designed for public use, and its arguments and return value are specified in local coordinates relative to the node's origin. You could use it with diagram's global coordinates like this:

Code
Select All
Point relativePoint1 = Node.TransformDiagramToItem(pt1);
Point relativePoint2 = Node.TransformDiagramToItem(pt2);
Point relativeIntersection = Node.GetIntersection(relativePoint1, relativePoint2);
var result =  Node.TransformItemToDiagram(relativeIntersection); 



It will return the node's center if the specified line does not intersect it, so you might need an additional check for that.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
mihai
Junior Member
**
Offline



Posts: 86
Joined: Jul 29th, 2009
Re: Understanding the result from ShapeNode.GetIntersection
Reply #2 - Sep 10th, 2014 at 2:38pm
Print Post  
Hi Stoyan,

Thanks for clarifying!

One last question: how do I find *all* the intersection points of a node with a line? I have tried to divide the hit segment by the intersection point, and call GetIntersection on each segment, but it always seems to return the same intersection point.

Best Regards,
-Mihai
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Understanding the result from ShapeNode.GetIntersection
Reply #3 - Sep 10th, 2014 at 3:47pm
Print Post  
Try this method:

Code
Select All
List<Point> FindIntersections(DiagramNode node, Point p1, Point p2)
{
	var bounds = node.Bounds;
	var intersection = new Point();
	var intersections = new List<Point>();
	var corners = new[]
	{
		bounds.TopLeft,
		bounds.TopRight,
		bounds.BottomRight,
		bounds.BottomLeft
	};
	for (int i = 0; i < 4; i++)
	{
		var c1 = corners[i];
		var c2 = corners[(i + 1) % 4];

		if (Utilities.SegmentIntersect(c1, c2, p1, p2, ref intersection))
			intersections.Add(intersection);

	}
	return intersections;
} 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
mihai
Junior Member
**
Offline



Posts: 86
Joined: Jul 29th, 2009
Re: Understanding the result from ShapeNode.GetIntersection
Reply #4 - Sep 10th, 2014 at 5:15pm
Print Post  
Hi Stoyan,

Cool, that should do it for the rectangle-shaped nodes.

Is there a way to do this more generically, to test for intersections with all the elements that make up a shape node (arc, line and Bezier segments)? Is ShapeNode.GetIntersection taking all these into account?

Thanks,
-Mihai
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Understanding the result from ShapeNode.GetIntersection
Reply #5 - Sep 11th, 2014 at 6:07am
Print Post  
This should work for non-rectangular shapes too:

Code
Select All
List<Point> FindIntersections(DiagramNode node, Point p1, Point p2)
{
	var bounds = node.Bounds;
	var geometry = node.GetRegion();
	geometry.Transform = new TranslateTransform(
		bounds.X, bounds.Y);

	var polygon = geometry.GetFlattenedPathGeometry().Figures[0];
	var vertices = new List<Point> { polygon.StartPoint };
	foreach (PolyLineSegment segment in polygon.Segments)
		vertices.AddRange(segment.Points);

	var last = vertices.Count - 1;
	if (vertices[0] == vertices[last])
		vertices.RemoveAt(last);

	var intersection = new Point();
	var intersections = new List<Point>();

	for (int i = 0; i < vertices.Count; i++)
	{
		var c1 = vertices[i];
		var c2 = vertices[(i + 1) % vertices.Count];

		if (Utilities.SegmentIntersect(c1, c2, p1, p2, ref intersection))
			intersections.Add(intersection);

	}
	return intersections;
} 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
mihai
Junior Member
**
Offline



Posts: 86
Joined: Jul 29th, 2009
Re: Understanding the result from ShapeNode.GetIntersection
Reply #6 - Sep 11th, 2014 at 10:53am
Print Post  
Thanks much Stoyan, it works like a charm!!
-Mihai
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint