Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic A question about Shape property of ShapeNode class (Read 1945 times)
CanadaProgrammer
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 113
Joined: Jun 30th, 2011
A question about Shape property of ShapeNode class
Apr 14th, 2013 at 11:38pm
Print Post  
I read the help document and there are several different places to show developers how to use Shape proeprty. All code in these smaples can work well. But if I just change a little it can not work well. Such as, there is a smaple in a chapter named Tutorial 7: Custom shape nodes. I copy these sample code and run it without any change, it can work, I show the image below:

and those code is:
node1.Shape = new Shape(
     new ElementTemplate[] {
     new LineTemplate(0, 0, 100, 0),
     new LineTemplate(100, 0, 100, 50),
     new LineTemplate(100, 50, 50, 50),
     new LineTemplate(50, 50, 50, 100),
     new LineTemplate(50, 100, 0, 100),
     new LineTemplate(0, 100, 0, 50)
},
     FillMode.Winding, "Custom");

But if I change the coordinate of line like below then it can not work:
node1.Shape = new Shape(
new ElementTemplate[] {
new LineTemplate(0, 0, 76, 0),
new LineTemplate(76, 0, 76, 22),
new LineTemplate(76, 22, 22, 22),
new LineTemplate(22, 22, 22, 76),
new LineTemplate(22, 76, 0, 76),
new LineTemplate(0, 76, 0, 22)
},
FillMode.Winding, "Custom");
and the image is below:


look at it, these adjustment handlers do not surround the whole shape exactly, this is the problem. In my case, all lines that consist outline of a shape are vertical or horizontal line, and I need to set any value to start poit and end point of a line. How to solve the problem and do it? Thanks a lot
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: A question about Shape property of ShapeNode class
Reply #1 - Apr 15th, 2013 at 7:07am
Print Post  
Coordinates in shape definitions are specified as percents of nodes' width and height, i.e. the 76 from your code as % means 3/4 of the node's width. If you need to create shapes from list of arbitrary points, you will have to find the bounding rectangle of the points and map them to a 0-100 coordinate system:

Code
Select All
Shape ShapeFromPoints(List<PointF> points, string id)
{
	float minX = float.MaxValue;
	float minY = float.MaxValue;
	float maxX = float.MinValue;
	float maxY = float.MinValue;

	foreach (var point in points)
	{
		minX = Math.Min(minX, point.X);
		minY = Math.Min(minY, point.Y);
		maxX = Math.Max(maxX, point.X);
		maxY = Math.Max(maxY, point.Y);
	}

	float w = maxX - minX;
	float h = maxY - minY;

	var elements = new ElementTemplate[points.Count];
	for (int i = 0; i < points.Count; i++)
	{
		var p1 = points[i];
		var p2 = points[(i + 1) % points.Count];
		elements[i] = new LineTemplate(
			100 * (p1.X - minX) / w, 100 * (p1.Y - minY) / h,
			100 * (p2.X - minX) / w, 100 * (p2.Y - minY) / h);
	}
	return new Shape(elements, FillMode.Winding, id);
} 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
CanadaProgrammer
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 113
Joined: Jun 30th, 2011
Re: A question about Shape property of ShapeNode class
Reply #2 - Apr 15th, 2013 at 5:30pm
Print Post  
Perfect,thanks.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint