Hello. I am trying to write a condition that will test if the mouse heading is within an angle range, and I’m having trouble figuring out how to make it work for cases when the angle range spans 0.
The idea is that an arc is drawn in the segment where the mouse is between two points. I tried to simplify it as much as I could, while still making it easy to see what’s not working.
Segment[] segs;
PVector c, m, mo, mCenter;
PVector[] sPoints;
float[] sAngles;
float r, moHeading, sAngle, sAOffset;
int nPts, nSegments;
void setup() {
size(400, 600);
ellipseMode(RADIUS);
m = new PVector();
mCenter = new PVector();
c = new PVector(width/2, height/2);
r = 150;
nSegments = 4;
nPts = nSegments;
sAngle = TWO_PI / nSegments;
sAOffset = -PI/2 - sAngle/2;
sAngles = new float[nSegments];
sPoints = new PVector[nPts];
for (int i = 0; i < sAngles.length; i++) {
float a = sAOffset + i*sAngle;
a = adjAngRange(a);
sAngles[i] = a;
}
for (int i = 0; i < sPoints.length; i++) {
float a = sAOffset + i*sAngle;
a = adjAngRange(a);
sPoints[i] = new PVector(c.x+cos(a)*r, c.y+sin(a)*r);
}
segs = new Segment[nSegments];
for (int i=0; i<segs.length; i++) {
segs[i] = new Segment();
}
}
void draw() {
background(127);
m.set(mouseX, mouseY);
mo = getMoffset(m, c);
mo.limit(r);
moHeading = getHeadingFromPt(mo, mCenter);
moHeading = adjAngRange(moHeading);
for (int i=0; i<sAngles.length; i++) {
float startA = sAngles[i];
float endA = sAngles[i] + sAngle;
//////////////////
if (moHeading > startA && moHeading < endA) {
fill(255);
arcPtR(c, r, startA, endA);
}
//////////////////
}
for (int i=0; i<sAngles.length; i++) {
float a = sAngles[i];
lineAngR(c, a, r);
}
for (int i=0; i<sPoints.length; i++) {
String notationStr = nf(i, 1);
textDot(notationStr, sPoints[i]);
}
linePtPt(c, PVector.add(c, mo));
arrayToText(sAngles, moHeading);
}
PVector getMoffset(PVector a, PVector b) {
return PVector.sub(a, b);
}
class Segment {
}
float adjAngRange(float a) {
return (a < 0) ? TWO_PI + a : a;
}
float getMagFromPt(PVector magOf, PVector fromPt) {
return dist(magOf.x, magOf.y, fromPt.x, fromPt.y);
}
float getHeadingFromPt(PVector headingOf, PVector fromPt) {
PVector tempVec = PVector.sub(headingOf, fromPt);
return tempVec.heading();
}
void arcPtR(PVector xy, float radius, float start, float end) {
arc(xy.x, xy.y, radius, radius, start, end);
}
void linePtPt(PVector a, PVector b) {
line(a.x, a.y, b.x, b.y);
}
void lineAngR(PVector a, float angle, float r) {
line(a.x, a.y, a.x+cos(angle)*r, a.y+sin(angle)*r);
}
void circlePtR(PVector a, float radius) {
ellipse(a.x, a.y, radius, radius);
}
void textDot(String s, PVector loc) {
fill(255, 0, 0);
circlePtR(loc, 15);
fill(255);
textSize(15);
textAlign(CENTER, CENTER);
text(s, loc.x, loc.y);
}
void arrayToText(float[] array, float angle) {
textAlign(LEFT, TOP);
String angleStr = nf(round(degrees(angle)), 3);
text(angleStr, 5, 10);
for (int i=0; i<array.length; i++) {
String s = nf(i, 1) + ": " + nf(round(degrees(array[i])), 3);
text(s, 5, (i*20)+30);
}
}