This code draws a star shape :
public void draw(double radius, Color color)
{
int x = 0, y = Convert.ToInt32(radius);
double d = (5 / 4) - radius;
circlePoint(x, y, color);
while (x < y)
{
if (d < 0)
{
d += Math.Pow(x, 2) + 3;
x++;
y--;
}
else
{
d += (x - y) * 2 + 5;
y--;
}
circlePoint(x, y, color);
}
}
The drawn shape is shown below
The Actual Question: I want to write a method (bool hasPoint(Point p) ) to check if p is inside this shape. I know for other shapes like a circle or an ellipse, we can check the point's x and y in relative to the object's formula. My Question is "How can I find the formula using the algorithm in draw() ?
Might be helpful: I was messing around with the formula of circle and ellipse and I encountered this shape. It's obvious that it has a relation with those shapes formula.
EDIT:
This is not a polygon. You can see this as a deformed circle (an ellipse actually). In a circle, if p.X-x0^2 + p.Y-y0^2 is less than radius^2, then p is a point inside. You can easily tell that it is the formula of a cirlce: x^2 + y^2 = r^2 .
EDIT 2:
( (x0,y0) is the center point )
void circlePoint(int x, int y, Color foreColor)
{
putPixel(x + x0, y + y0);
putPixel(y + x0, x + y0);
putPixel(-x + x0, y + y0);
putPixel(-y + x0, x + y0);
putPixel(x + x0, -y + y0);
putPixel(y + x0, -x + y0);
putPixel(-x + x0, -y + y0);
putPixel(-y + x0, -x + y0);
}
void putPixel(int x, int y, Color color){ bitmap.SetPixel(x, y, color); }
EDIT3:
It appears that this curve is mirrored by 8 lines (2 horizontal, 2 vertical and 4 diagonal):
EDIT4:
Based on emrgee's answer I've added these 2 functions, I don't know where I am wrong but it seems hasPointInside() never returns true;
public double contour(double x)
{
double x2 = Math.Pow(x,2);
return -0.190983 + 0.427051 * Math.Sqrt(0.923607 + (0.647214 * x) - (1.37082 * x2));
}
public bool hasPointInside(Point p)
{
int min = Math.Min(Math.Abs(p.X), Math.Abs(p.Y));
int max = Math.Max(Math.Abs(p.X), Math.Abs(p.Y));
return min <= radius * contour(max / radius);
}