public static ArrayList<IntPoint> getCircleLineIntersectionPoint(IntPoint pointA, IntPoint pointB, IntPoint center, int radius) {
// returns a list of intersection points between a line which passes through given points,
// pointA and pointB, and a circle described by given radius and center coordinate
double disc, A, B, C, slope, c;
double x1, x2, y1, y2;
IntPoint point1, point2;
ArrayList<IntPoint> intersections = new ArrayList<IntPoint>();
try{
slope = Util.calculateSlope(pointA, pointB);
}catch (UndefinedSlopeException e){
C = Math.pow(center.y, 2) + Math.pow(pointB.x, 2) - 2 * pointB.x * center.x + Math.pow(center.x, 2) - Math.pow(radius, 2);
B = -2 * center.y;
A = 1;
disc = Math.pow(B, 2) - 4 * 1 * C;
if (disc < 0){
return intersections;
}
else{
y1 = (-B + Math.sqrt(disc))/(2 * A);
y2 = (-B - Math.sqrt(disc))/(2 * A);
x1 = pointB.x;
x2 = pointB.x;
}
point1 = new IntPoint((int)x1, (int)y1);
point2 = new IntPoint((int)x2, (int)y2);
if (Util.euclideanDistance(pointA, point2) > Util.euclideanDistance(pointA, point1)){
intersections.add(point1);
}
else{
intersections.add(point2);
}
return intersections;
}
if (slope == 0){
C = Math.pow(center.x, 2) + Math.pow(center.y, 2) + Math.pow(pointB.y, 2) - 2 * pointB.y * center.y - Math.pow(radius, 2);
B = -2 * center.x;
A = 1;
disc = Math.pow(B, 2) - 4 * 1 * C;
if (disc < 0){
return intersections;
}
else{
x1 = (-B + Math.sqrt(disc))/(2*A);
x2 = (-B - Math.sqrt(disc))/(2*A);
y1 = pointB.y;
y2 = pointB.y;
}
}
else{
c = slope * pointA.x + pointA.y;
B = (2 * center.x + 2 * center.y * slope + 2 * c * slope);
A = 1 + Math.pow(slope, 2);
C = (Math.pow(center.x, 2) + Math.pow(c, 2) + 2 * center.y * c + Math.pow(center.y, 2) - Math.pow(radius, 2));
disc = Math.pow(B, 2) - (4 * A * C);
if (disc < 0){
return intersections;
}
else{
x1 = (-B + Math.sqrt(disc))/(2 * A);
x2 = (-B - Math.sqrt(disc))/(2 * A);
y1 = slope * x1 - c;
y2 = slope * x2 - c;
}
}
point1 = new IntPoint((int)x1, (int)y1);
point2 = new IntPoint((int)x2, (int)y2);
if (Util.euclideanDistance(pointA, point2) > Util.euclideanDistance(pointA, point1)){
//if (Util.angleBetween(pointA, pointB, point1) < Math.PI/2){
intersections.add(point1);
//}
}
else{
//if (Util.angleBetween(pointA, pointB, point1) < Math.PI/2){
intersections.add(point2);
//}
}
return intersections;
}
मैं उपरोक्त एल्गोरिदम का उपयोग एक सर्कल और रेखा के बीच चौराहे के परीक्षण के लिए कर रहा हूं। यह कभी-कभी ठीक काम करता है लेकिन दूसरी बार यह विफल रहता है। कोड समीकरण का प्रतिनिधित्व करता है जो एक्स के साथ सर्कल और लाइन समीकरण (x-a)^+(y-b)^2=r^2
और y = mx - mx1 + y1
से एक साथ हल करने से लिया गया है। क्या किसी को यह पता चला है कि मैं अपने गणित या अन्य जगहों में गलत कहां जा रहा हूं?सर्किल लाइन छेड़छाड़ अंक
के साथ एक समाधान है कि आप कुछ लाइनों और हलकों कि कारण यह विफल करने का एक उदाहरण दे सकते हैं? और आप अपने एक्स, वाई निर्देशांक को पूर्णांक में क्यों परिवर्तित करते हैं? –
क्या आप अपने वेरिएबल घोषित करने का प्रयास कर सकते हैं जहां आप उनका उपयोग करते हैं, और कहीं और पूरी तरह से नहीं? और उन्हें और अधिक सार्थक नाम दें? ('ढलान 'एक अच्छी शुरुआत है, हालांकि।) इसके अलावा, मैं उन बिंदुओं की अपेक्षा भी नहीं करूंगा जो आप पूर्णांक निर्देशांक रखने के लिए देख रहे हैं, इसलिए कास्टिंग बहुत संदिग्ध प्रतीत होता है। –
इंटपॉइंट क्लास का इस्तेमाल लाइब्रेरी द्वारा किया जाता है और निर्देशांक int – cobie