circle line intersection+rotation
in
Programming Questions
•
5 months ago
hi, here's code for circle line intersection
floatcircleX = 200;floatcircleY = 200;floatcircleRadius = 100;floatlineX1 = 350;floatlineY1 = 350;floatlineX2, lineY2;voidsetup() {size(400, 400);ellipseMode(RADIUS);smooth();}voiddraw() {background(204);lineX2 =mouseX;lineY2 =mouseY;if(circleLineIntersect(lineX1, lineY1, lineX2, lineY2, circleX, circleY, circleRadius) ==true) {fill(0);}else{fill(255);}ellipse(circleX, circleY, circleRadius, circleRadius);line(lineX1, lineY1, lineX2, lineY2);}// Code adapted from Paul Bourke:// http://local.wasp.uwa.edu.au/~pbourke/geometry/sphereline/raysphere.cbooleancircleLineIntersect(floatx1,floaty1,floatx2,floaty2,floatcx,floatcy,floatcr ) {floatdx = x2 - x1;floatdy = y2 - y1;floata = dx * dx + dy * dy;floatb = 2 * (dx * (x1 - cx) + dy * (y1 - cy));floatc = cx * cx + cy * cy;c += x1 * x1 + y1 * y1;c -= 2 * (cx * x1 + cy * y1);c -= cr * cr;floatbb4ac = b * b - 4 * a * c;//println(bb4ac);if(bb4ac < 0) {// Not intersectingreturnfalse;}else{floatmu = (-b +sqrt( b*b - 4*a*c )) / (2*a);floatix1 = x1 + mu*(dx);floatiy1 = y1 + mu*(dy);mu = (-b -sqrt(b*b - 4*a*c )) / (2*a);floatix2 = x1 + mu*(dx);floatiy2 = y1 + mu*(dy);// The intersection points//ellipse(ix1, iy1, 10, 10);//ellipse(ix2, iy2, 10, 10);floattestX;floattestY;// Figure out which point is closer to the circleif(dist(x1, y1, cx, cy) <dist(x2, y2, cx, cy)) {testX = x2;testY = y2;}else{testX = x1;testY = y1;}if(dist(testX, testY, ix1, iy1) <dist(x1, y1, x2, y2) ||dist(testX, testY, ix2, iy2) <dist(x1, y1, x2, y2)) {returntrue;}else{returnfalse;}}}
i wanted to rotate line instead moving with mouse but this breaks the code and intersections don't work anymore, anyone can help me out with this please?
- float circleX = 200;
float circleY = 50;
float circleRadius = 20;
float lineX1 = 0;
float lineY1 = 0;
float lineX2, lineY2;
float a=0;
void setup() {
size(400, 400);
ellipseMode(RADIUS);
smooth();
}
void draw() {
background(204);
lineX2 = 150;
lineY2 = 0;
if (circleLineIntersect(lineX1, lineY1, lineX2, lineY2, circleX, circleY, circleRadius) == true) {
fill(0);
}
else {
fill(255);
}
ellipse(circleX, circleY, circleRadius, circleRadius);
//rotating the line
pushMatrix();
translate(150,150);
rotate(a);
line(lineX1, lineY1, lineX2, lineY2);
popMatrix();
a+=0.1;
}
// Code adapted from Paul Bourke:
// http://local.wasp.uwa.edu.au/~pbourke/geometry/sphereline/raysphere.c
boolean circleLineIntersect(float x1, float y1, float x2, float y2, float cx, float cy, float cr ) {
float dx = x2 - x1;
float dy = y2 - y1;
float a = dx * dx + dy * dy;
float b = 2 * (dx * (x1 - cx) + dy * (y1 - cy));
float c = cx * cx + cy * cy;
c += x1 * x1 + y1 * y1;
c -= 2 * (cx * x1 + cy * y1);
c -= cr * cr;
float bb4ac = b * b - 4 * a * c;
//println(bb4ac);
if (bb4ac < 0) { // Not intersecting
return false;
}
else {
float mu = (-b + sqrt( b*b - 4*a*c )) / (2*a);
float ix1 = x1 + mu*(dx);
float iy1 = y1 + mu*(dy);
mu = (-b - sqrt(b*b - 4*a*c )) / (2*a);
float ix2 = x1 + mu*(dx);
float iy2 = y1 + mu*(dy);
// The intersection points
//ellipse(ix1, iy1, 10, 10);
//ellipse(ix2, iy2, 10, 10);
float testX;
float testY;
// Figure out which point is closer to the circle
if (dist(x1, y1, cx, cy) < dist(x2, y2, cx, cy)) {
testX = x2;
testY = y2;
} else {
testX = x1;
testY = y1;
}
if (dist(testX, testY, ix1, iy1) < dist(x1, y1, x2, y2) || dist(testX, testY, ix2, iy2) < dist(x1, y1, x2, y2)) {
return true;
} else {
return false;
}
}
}
1
