All you really need is a list of pixels in one quadrant of the hexagon. Then you can simply "reflect" the x and y coordinates to get the full hexagon (subject to screen bounds of course).
First, I would make the assertion that I want one of my hexagon sides to be horizontally aligned. I would also make the assumption that by "size of my hexagon" I mean the length (let's call is L) of the vertical line from the center of my hexagon to the bottom side (which is horizontally aligned). Then, I would do algebra and trig on a hexagon of this alignment with size L, assuming that the origin (0,0) is my center point.
I know, then that all of the points within the bounds [0,0,L/sqrt(3),L]
(that is [x-offset, y-offset, width, height]) are definitely within my hexagon. So add all these points to my list.
List<Point> pointsInHexagonQuadrant = new List<Point>();
for (int i = 0; i < L/Math.Sqrt(3); i++) //I'm ignoring any casting, you may have to fix.
{
for (int j = 0; j <= L; j++)
{
pointsInHexagonQuadrant.Add(new Point(i,j));
}
}
I know by trig and algebra that the right-most point of my hexagon is at (2*L/sqrt(3),0) and from L/sqrt(3) to 2*L/sqrt(3) the equation of the hexagon's sloped side is y=sqrt(3)*x-2*L
. I want all the points whose y coordinate is less than that.
for(int i = L/Math.Sqrt(3); i <= 2*L/Math.Sqrt(3); i++)
{
for (int j = 0; j <= Math.Sqrt(3)*i-2*L; j++)
{
pointsInHexagonQuadrant.Add(new Point(i,j));
}
}
Add this point you have one quadrant of the hexagon, like this:
(0,0) (2L/sqrt(3),0)
---------------
| /
| /
| /
| /
| /
|-------/
(0,L) (L/sqrt(3),L)
To get the full hexagon, you "reflect" across the x and y axes...
List<Point> pointsInMyHexagon = new List<Point>();
foreach (Point p in pointsInHexagonQuadrant)
{
pointsInMyHexagon.Add(new Point(p.X,p.Y));
pointsInMyHexagon.Add(new Point(-p.X,p.Y));
pointsInMyHexagon.Add(new Point(p.X,-p.Y));
pointsInMyHexagon.Add(new Point(-p.X,-p.Y));
}
Now offset the hexagon to put the center back on your (x,y) point.
foreach (Point p in pointsInMyHexagon)
{
p.Offset(myCenterPoint.X, myCenterPoint.Y);
}
It might be crude but the concept should work.