I'm trying to highlight pixels that fall within a sector of a circle. I'm writing a shader to do this, but I'm implementing the logic in JavaScript until I get it right.
Essentially, each pixel coordinate in a canvas is scaled to be between 0 and 1, and is passed into the following code along with the canvas context:
function isWithinSector(ctx, x, y) {
let startAngle = degToRad(135), endAngle = degToRad(205);
// Distance of pixel from the circle origin (0.5, 0.5).
let dx = scaledX - 0.5;
let dy = scaledY - 0.5;
let angle = Math.atan2(dy, dx);
if (angle >= startAngle && angle <= endAngle) {
ctx.fillStyle = "rgba(255, 255, 0, .5)";
ctx.fillRect(x, y, 1, 1);
}
}
This works fine for some angles, but not for others. Pixels highlighted between 135 and 205 degrees appear like this (i.e. only 135 to 180 degrees are highlighted):
Note that the highlighted pixels don't match my black arc (the source of truth). I've been trying all kinds of things from Google but I'm stuck.
I have a CodePen that shows the issue: https://codepen.io/chrisparton1991/pen/XRpqXb. Can anybody guide me on what I'm doing wrong in my algorithm?
Thanks!