Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Vertex Finding (Read 2358 times)
jstraylor
Junior Member
**
Offline


I love YaBB 1G - SP1!

Posts: 50
Joined: Dec 28th, 2006
Vertex Finding
Dec 28th, 2006 at 6:54pm
Print Post  
Hello all,

I have a need to be able to find the nearest vertex of a custom shape (any of the predefined Arrow shapes would be good example) in order to draw lines from that vertex position.  I need to be able to do this regardless of the rotation angle of the shape.

Is there an easy way to do this, or maybe a way that has already been coded??

Basically the user is going to right click on a box to bring up a context menu that will allow them to enter "Measure Mode" from the closest vertex.  I will be drawing arrows from that vertex out to the top and right boundaries of the containing box and presenting the user with the distance from the vertex to the boundaries.

Thank you,
Jeff
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Vertex Finding
Reply #1 - Dec 29th, 2006 at 6:38am
Print Post  
Hi,

This method will find the shape vertices -

Code
Select All
PointCollection GetVertices(ShapeTemplate shape, RectangleF bounds, float angle)
{
	PointCollection vertices = new PointCollection(0);
	PointF center = new PointF(
		bounds.X + bounds.Width / 2, bounds.Y + bounds.Height / 2);

	foreach (ElementTemplate element in shape.Outline)
	{
		if (element is LineTemplate)
		{
			LineTemplate line = element as LineTemplate;
			vertices.Add(new PointF(
				bounds.X + line.Coordinates[0] * bounds.Width / 100,
				bounds.Y + line.Coordinates[1] * bounds.Height / 100));
		}
		if (element is BezierTemplate)
		{
			BezierTemplate bezier = element as BezierTemplate;
			vertices.Add(new PointF(
				bounds.X + bezier.Coordinates[0] * bounds.Width / 100,
			bounds.Y + bezier.Coordinates[1] * bounds.Height / 100));
		}
		if (element is ArcTemplate)
		{
			ArcTemplate arc = element as ArcTemplate;
			float ellipseWidth = arc.Bounds.Width * bounds.Width / 100;
			float ellipseHeight = arc.Bounds.Height * bounds.Height / 100;
			vertices.Add(new PointF(
				(float)(center.X + ellipseWidth * Math.Cos(arc.StartAngle) / 2),
				(float)(center.Y + ellipseHeight * Math.Sin(arc.StartAngle) / 2)));
		}
	}

	PointF[] vertexArray = vertices.GetArray();
	RotatePointsAt(vertexArray, center, angle);
	return new PointCollection(vertexArray);
}

void RotatePointsAt(PointF[] points, PointF pivot, float angle)
{
	Matrix rotation = new Matrix();
	rotation.RotateAt(angle, pivot);
	rotation.TransformPoints(points);
	rotation.Dispose();
}

private void flowChart1_BoxDblClicked(object sender, MindFusion.Diagramming.WinForms.BoxMouseArgs e)
{
	if (e.Box.Style == BoxStyle.Shape)
	{
		PointCollection vertices = GetVertices(
			e.Box.Shape, e.Box.BoundingRect, e.Box.RotationAngle);

		// show the vertices
		PointF origin = new PointF();
		foreach (PointF point in vertices)
		{
			fc.CreateArrow(origin, point);
		}
	}
}
 



Compare the distances between your point and all vertices to find the nearest one.

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


I love YaBB 1G - SP1!

Posts: 50
Joined: Dec 28th, 2006
Re: Vertex Finding
Reply #2 - Dec 29th, 2006 at 1:33pm
Print Post  
Thanks so much!  That looks like it should do the trick.

I'll let you know, and also offer the suggestion that this become a method on the shape (5.0??)  Grin

Jeff
  
Back to top
 
IP Logged
 
jstraylor
Junior Member
**
Offline


I love YaBB 1G - SP1!

Posts: 50
Joined: Dec 28th, 2006
Re: Vertex Finding
Reply #3 - Dec 29th, 2006 at 2:05pm
Print Post  
Good Morning,

I probably goofed by not specifying that I'm using VB.NET and Flowchart.NET 4.2.2.  I converted the code to VB, but have a couple of questions.

1.  I assume that the Matrix being used is from system.drawing.drawing2d (that being MUCH more logical than any of the windows mobile libraries)

2. The declaration of BoxDblClicked expects Mindfusion.Diagramming.Winforms.BoxMouseArgs, I changed this to MindFusion.FlowChartX.BoxMouseArgs.  Was this code for, or from, a newer version??

3. The GetVertices function returns a PointCollection, but the PointCollection constructor expects an integer, rather than the vertexArray that is being passed in.  Is there another constructor that I'm not aware of, or should I just create the collection and add the points before returning.

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


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Vertex Finding
Reply #4 - Dec 29th, 2006 at 5:01pm
Print Post  
Good evening from here  8)

Quote:
1.  I assume that the Matrix being used is from system.drawing.drawing2d (that being MUCH more logical than any of the windows mobile libraries)


That's right.

Quote:
2. The declaration of BoxDblClicked expects Mindfusion.Diagramming.Winforms.BoxMouseArgs, I changed this to MindFusion.FlowChartX.BoxMouseArgs.  Was this code for, or from, a newer version??


It was for version 4.2.2, so I guess you are still using the flowchart.net.dll from an older version.

Quote:
3. The GetVertices function returns a PointCollection, but the PointCollection constructor expects an integer, rather than the vertexArray that is being passed in.  Is there another constructor that I'm not aware of, or should I just create the collection and add the points before returning.


This too might be something new for Flowchart.NET 4.2.2. Try calling the AddRange method if you don't have such constructor in your version.

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