7. Circle Intersection. Write a program that computes the intersection of a circ
ID: 3592811 • Letter: 7
Question
7. Circle Intersection. Write a program that computes the intersection of a circle with a hori- zontal line and displays the information textually and graphically Input: Radius of the circle and the y-intercept of the line. Output: Draw a circle centered at (0, 0) with the given radius in a windovw with coordinates running from -10,-10 to 10,10. Draw a horizontal line across the window with the given y-intercept. Draw the two points of intersection in red. Print out the a values of the points of intersection.Explanation / Answer
class Point{ public: float x, y; Point(float px, float py) { x = px; y = py; } Point sub(Point p2) { return Point(x - p2.x, y - p2.y); } Point add(Point p2) { return Point(x + p2.x, y + p2.y); } float distance(Point p2) { return sqrt((x - p2.x)*(x - p2.x) + (y - p2.y)*(y - p2.y)); } Point normal() { float length = sqrt(x*x + y*y); return Point(x/length, y/length); } Point scale(float s) { return Point(x*s, y*s); } }; class Circle { public: float x, y, r, left; Circle(float cx, float cy, float cr) { x = cx; y = cy; r = cr; left = x - r; } pair intersections(Circle c) { Point P0(x, y); Point P1(c.x, c.y); float d, a, h; d = P0.distance(P1); a = (r*r - c.r*c.r + d*d)/(2*d); h = sqrt(r*r - a*a); Point P2 = P1.sub(P0).scale(a/d).add(P0); float x3, y3, x4, y4; x3 = P2.x + h*(P1.y - P0.y)/d; y3 = P2.y - h*(P1.x - P0.x)/d; x4 = P2.x - h*(P1.y - P0.y)/d; y4 = P2.y + h*(P1.x - P0.x)/d; return pair(Point(x3, y3), Point(x4, y4)); } };Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.