If statement seems to be skipping to else
I'm trying to write a program that decides whether a circle is
inside/touching a rectangle. The user puts in the center point for the
circle and the radius, and two diagonal points for the rectangle.
I'm not sure how to include all points of the circumference of the circle,
to tell that there is at least one point in/touching the rectangle. Anyone
sure how to do this?
When I run my current program, I'll purposely enter points of a circle
being inside of a rectangle, and should work with the if statements I put,
but it prints out the wrong answer.
import java.util.Scanner;
public class lab4 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double cx, cy, x, y, r, p1x, p1y, p2x, p2y, max;//input
String a;
System.out.print("Enter cx: ");
cx = in.nextDouble();
System.out.print("Enter cy: ");
cy = in.nextDouble();
System.out.print("Enter r: ");
r = in.nextDouble();
System.out.println("Enter x value of point 1:");
p1x = in.nextDouble();
System.out.println("Enter y value of point 1:");
p1y = in.nextDouble();
System.out.println("Enter x value of point 2:");
p2x = in.nextDouble();
System.out.println("Enter y value of point 2:");
p2y = in.nextDouble();
max = p2x;
if (p1x > max)
max = p1x;
max = p2y;
if (p1y > max)
max = p1y;
if (cx >= p1x && cx <= p2x)
a = "Circle is inside of Rectangle";
if (cx >= p1x && cx <= p2x)
a = "Circle is inside of Rectangle";
if (cx+r >= p1x && cx+r <= p2x)
a = "Circle is inside of Rectangle";
if (cx-r >= p1x && cx-r <= p2x)
a = "Circle is inside of Rectangle";
if (cy >= p1y && cy <= p2y)
a = "Circle is inside of Rectangle";
if (cy >= p1y && cy <= p2y)
a = "Circle is inside of Rectangle";
if (cy+r >= p1y && cy+r <= p2y)
a = "Circle is inside of Rectangle";
if (cy-r >= p1y && cy-r <= p2y)
a = "Circle is inside of Rectangle";
else
a = "Circle is outside of Rectangle";
System.out.println(a);
No comments:
Post a Comment