Filling an area of piece of circle in square grid

I am trying to fill some particular circle areas on a square grid in Proceesing. However, I am facing some weird filling after I run my code. I used the "atan2" function to get the angle of points in the grid and apply some if conditions to limit the area. But it doesn't work. Actually, it works some way but not exactly what I want.This is the result I run the code-> enter image description here, but it should be like -> enter image description here Can someone help me to solve this, please?

(Additionally, I seized this page to detect which points are in the area I specified enter link description here.

Cell [][] cells;
int res = 10;

PVector circleCenter ;
float rad=290;

void setup() {
  size(1200, 800);
  cells = new Cell[width/res ][height/res];
  for (int i=0; i<cells.length; i++) {
    for (int j = 0; j<cells[0].length; j++) {
      cells[i][j] = new Cell();
    }
  }
}


void draw() {
  background(255);
  circleCenter = new PVector(mouseX, mouseY);
  display();
  pushStyle();
  ellipse(circleCenter.x, circleCenter.y, 20, 20);
  popStyle();
  //println(frameRate);
}



void display() {                                                                            
for (int i=0; i<cells.length; i++) {
for (int j = 0; j<cells[0].length; j++) {
  if (sq(i*res-width/2) + sq(j*res-height/2) <=sq(rad/2)) { 
    float angleInRad = atan2(j*res-height/2, i*res-width/2);
    ///
    if (angleInRad<radians(-10) && angleInRad>radians(-60)) {
      fill(0, 255, 0); // degrees(atan2(j*res-circleCenter.y, i*res-circleCenter.x))
    }
  } else {
    noFill();
  }      
  rect(i*res, j*res, res, res);
}}

    class Cell {  
boolean blackness=false;
    Cell() {
    }
    }

Upvotes: 0

Views: 190

Answers (1)

MBo
MBo

Reputation: 80187

You have not specified input data for your task. I assume we have center coordinates cx, cx, radius r, starting and ending angle sa, ea of the sector (in code -10, -60).

There is an approach to check whether vector direction lies in sector, it's robust to potential troubles with periodicity, negative values etc. At first normalize range ends, then find middle angle and half-angle

half = (ea - sa) / 2
mid  = (ea + sa) / 2
coshalf = Cos(half)

Now compare that difference of angle and middle one is lower then half-angle

if Cos(angle - mid) >= coshalf then   
   angle lies in range sa..ea

Upvotes: 0

Related Questions