Reputation: 1233
I need to draw a solid polygon in memory (into a 2D array) and 'fill' the polygon with numeric values (say '3').
I wish to do this in C#.
I'm getting the solid polygons from Shapefiles using Catfood's Shapefile reader (very good).
Any ideas?
I'm attaching a small portion of this 2D array after I've already 'mapped' 16,000 polylines that represent that road network around San Diego (they appear as the number '9'). I wish to do the same thing by uploading a shapefile of solid polygons and 'drawing' with the number '3'.
Upvotes: 0
Views: 1498
Reputation: 8656
Since you are drawing in the 2D array of ints (as far as I can see after you have edited your question) it seems like you should implement your own polygon fill that is storing the numbers in the 2D array. For this purpose you could use this post Good algorithm for drawing solid 2-dimensional polygons?
The other solution is a little workaround. You could use the already implemented PolygonFill
that fills polygons in the Bitmap. Check this out. I must warn you that getting bitmap pixel is very slow and for that purposes you can use some FastBitmap implementation. Here I will use regular Bitmap.
Bitmap bmp = new Bitmap(0,0,mostRightPoint.X - mostLeftPoint.X, mostUpperPoint.Y - mostLowerPoint.Y);
//this implies that you are on the northern hemisphere
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White); //clear the whole bitmap into white color
int [] points = new points[nmbOfPoints];
for(int i = 0;i<nmbOfPoints;i++)
{
//put the points in the points array
}
g.FillPolygon(Brushes.Black, points);
g.Dispose();
Now you should iterate through the Bitmap, on those places where the pixel is black, put the number 3 in your 2D array, somethink like this.
for(int i = 0;i<bmp.Width;i++)
for(int j = 0;j<bmp.Height;j++)
if(Bitmap.GetPixel(i,j) == Color.Black)
{
2DArray[mostLeftPoint.X + i, mostLowerPoint.Y + j] = 3;
}
I think you got the sense of the problem and a possible solution.
Upvotes: 0
Reputation: 26398
Go grab the WriteableBitmapEx Extension. That will allow you to draw just about anything you want into the image memory.
Alternatively, you can make a DrawingVisual, draw whatever you want with it, then render to an image target; See: This example
if you want to go via the System.Drawing route;
using System.Drawing;
Bitmap bmp = new Bitmap(200, 100);
Graphics g = Graphics.FromImage(bmp);
g.DrawLine(Pens.Black, 10, 10, 180, 80);
REF:(Henk Holterman) Drawing C# graphics without using Windows Forms
But I suspect (based on the wording) this is homework, and you've been told to do it manually;
So; for lines you want Bresenham's Line Algorithm, and then fill them with a fill Algorithm; See This
Upvotes: 1
Reputation: 79441
Create a Bitmap
, get a Graphics
from it, call FillPolygon
on the Graphics
.
Upvotes: 1
Reputation: 44288
in C# you can use the Bitmap class to do offscreen drawing of whatever you wish.
Upvotes: 1