Reputation:
I want to draw the following red polygon:
The problem is if I use somethign like this:
Polygon poly = new Polygon();
poly.StrokeThickness = 2;
poly.Stroke = Brushes.Black;
PointCollection points = new PointCollection();
for (int i = 0; i < this.NumberOfMetrics; i++)
{
points.Add(new Point(MAX_VALUE - this.Metrics[n, i] * Math.Cos(DegreeToRadian(i * (360 / (this.NumberOfMetrics)))), MAX_Y_GUI - this.Metrics[n, i] * Math.Sin(DegreeToRadian(i * (360 / (this.NumberOfMetrics))))));
}
poly.Points = points;
Then the polygon is always "filled" and in the example above the red and green polygon is drawn.
I already tried to add the 4 "inner" points to the PointCollection, but then nothing is drawn. So how can I achieve that?
I tried the solution proposed by David:
for (int n = 0; n < this.NumberOfRevisions; n++)
{
Path path = new Path();
CombinedGeometry geometry = new CombinedGeometry();
geometry.GeometryCombineMode = GeometryCombineMode.Union;
Polygon poly = new Polygon();
PointCollection points = new PointCollection();
for (int i = 0; i < this.NumberOfMetrics; i++)
{
points.Add(new Point(MAX_VALUE - this.Metrics[n, i] * Math.Cos(DegreeToRadian(i * (360 / (this.NumberOfMetrics)))), MAX_Y_GUI - this.Metrics[n, i] * Math.Sin(DegreeToRadian(i * (360 / (this.NumberOfMetrics))))));
}
poly.Points = points;
geometry.Geometry1 = poly.RenderedGeometry;
geometry.Geometry2 = poly.RenderedGeometry;
path.Data = geometry;
polygons.Add(poly);
paths.Add(path);
}
This is just a test but I thougth so I should get the same result as before, but it isn't drawn anything. Is there something wrong with my code?
Upvotes: 5
Views: 12095
Reputation: 6124
If you want to have 2 independent shapes, with the possibility of the green one to be transparent as you stated in your comment, the best way to do is to use a combined geometry:
http://msdn.microsoft.com/en-en/library/ms653071%28v=VS.85%29.aspx
with the help of this, you can first create the green geometry, then the red by subtracting the green (or a copy of it) from the red one to create the hole.
So basically:
this way you get the effect you want
easier done in Xaml, a bit more complicated in C# but still doable.
Edit: set the Combined Geometry as a Path's Data:
Path myPath = new Path();
CombinedGeometry myCombinedGeometry = new CombinedGeometry()
// here you set the combinedGeometry's geometries to create the shape you want
myPath.Data = myCombinedGeometry;
myGrid.Children.Add(myPath);
by the way, the PATH will be the place where you set the Fill / Stroke attribute for the colors, not the inside geometries. (see the examples in xaml in the link above, you basically just have to translate the code into C#)
Edit2:
don't forget to set a Fill on the Path:
for (int n = 0; n < this.NumberOfRevisions; n++)
{
CombinedGeometry geometry = new CombinedGeometry() { GeometryCombineMode = GeometryCombineMode.Union };
PointCollection points = new PointCollection();
for (int i = 0; i < this.NumberOfMetrics; i++)
{
points.Add(new Point(MAX_VALUE - this.Metrics[n, i] * Math.Cos(DegreeToRadian(i * (360 / (this.NumberOfMetrics)))), MAX_Y_GUI - this.Metrics[n, i] * Math.Sin(DegreeToRadian(i * (360 / (this.NumberOfMetrics))))));
}
Polygon poly = new Polygon();
poly.Points = points;
geometry.Geometry1 = poly.RenderedGeometry;
geometry.Geometry2 = poly.RenderedGeometry;
polygons.Add(poly);
paths.Add(path = new Path() { Data = geometry, Fill = Brushes.Red, Stroke = Brushes.Transparent });
}
Upvotes: 6