1

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: enter image description here 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

Output: enter image description here

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?

eques
  • 33
  • 4

1 Answers1

1

Instead of sectstartx you have to use

sectstartx - center_x

to form proper vector for cross product checking. Same for other sect*** values

MBo
  • 77,366
  • 5
  • 53
  • 86
  • not working. it just draws single line and nothing https://imgur.com/a/2qF5jXO – eques Nov 13 '22 at 13:17
  • i changed `sectstartx` in `isInsideSector` is that correct: `return ((sectstartx - center_x) * relpointy - relpointx * (sectstarty - center_y) > 0 && relpointx * (sectendy - center_y) - (sectendx -center_x) * relpointy > 0);` – eques Nov 13 '22 at 13:23
  • Looks correct. But check signs. They should be equal, but depend on start/end relation (so or both >=0, or both <=0) – MBo Nov 13 '22 at 14:41
  • what do you mean by relation? how to check that – eques Nov 13 '22 at 15:25
  • Start-> clockwise->end and Start-> counterclockwise->end give different signs. Just check result for ` <0` in your case – MBo Nov 13 '22 at 15:32
  • how to check are they clockwise or not? can you please edit your answer – eques Nov 13 '22 at 21:04
  • It depend on your coordinate system and on argument order. I suggest just to check if signs are equal – MBo Nov 14 '22 at 01:09