I have method drawSector
, where boolean function isInsideSector
checks if point in circle is inside sector and color it. But output sector is wrong with its coordinates, and I don't know where the problem is. Here is my logic and code:
public static void drawSector(
final GraphicsContext graphicsContext,
int center_x, int center_y, // center of circle (x,y)
int sectstartx, int sectstarty, // start point of sector
int sectendx, int sectendy, // end point of sector
int radius, // radius
Color c0) { // sector color
final PixelWriter pixelWriter = graphicsContext.getPixelWriter();
for (int x = -radius; x <= radius; x++) { // circle fill algorithm
int height = (int) Math.sqrt(radius * radius - x * x); // no radius check, high efficiency
for (int y = -height; y <= height; y++)
if (isInsideSector(x + center_x, y + center_y, center_x, center_y, sectstartx, sectstarty, sectendx, sectendy, radius)) {
pixelWriter.setColor(x + center_x, y + center_y, c0); // set point
}
}
}
private static boolean isInsideSector( // checks if point is inside certain sector
int x, int y, // point in circle (vector S)
int center_x, int center_y, // center of circle
int sectstartx, int sectstarty, // start point of sector (vector V1)
int sectendx, int sectendy, // end point of sector (vector V2)
int radius) { // radius
int relpointx = x - center_x; // because in drawSector x + center_x
int relpointy = y - center_y;
return (sectstartx * relpointy - relpointx * sectstarty > 0 && relpointx * sectendy - sectendx * relpointy > 0);
Visualisation of method isInsideSector
or what I mean to write, I'm so sorry if your eyes are bleeding:
How I create sector:
Rasterization.drawSector(canvas.getGraphicsContext2D(), 200, 200, 0, -4, -1, -4, 150, Color.BLUE)
// center of circle (200,200); sector start point (0,-4); sector end point (-1,-4); radius 150
I thought that the problem is in circle fill, but i tried with scanline fill and it is the same output. Maybe the problem is in my logic where I check sector point? Or with JavaFX coordinate grid?
Instead of sectstartx
you have to use
sectstartx - center_x
to form proper vector for cross product checking. Same for other sect*** values