Write a Java applet that first, asks the user to enter the type of the shape to
ID: 3591298 • Letter: W
Question
Write a Java applet that first, asks the user to enter the type of the shape to be drawn on the screen (circles, Rectangles, or lines). Then it reads the coordinates of the two objects of the selected shapes (diameter of two circles, and their center coordinates, or the coordinates of the top left corners of the rectangles and their widths and heights, or the coordinates of the vertices of the two lines). Your program will then display the perimeters and areas of the two 2D shapes or the lengths of the two lines as well as one of the following messages: 1. The two shapes do not overlap 2. The two shapes overlap, but none of them is fully inside the other one 3. The two shapes overlap, and shape (1 or 2) is inside of shape (2 or 1Explanation / Answer
package consoleProgram;
import java.util.Scanner;
public class CheggGeometry {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter Shape Type:1. Circle 2. Rectangle 3. Line ");
int sh = input.nextInt();
if(sh == 1){
System.out.println("Enter coordinate of circle 1");
double X1 = input.nextDouble();
double Y1 = input.nextDouble();
double dia1 = input.nextDouble();
System.out.println("Enter coordinate of circle 2");
double X2 = input.nextDouble();
double Y2 = input.nextDouble();
double dia2 = input.nextDouble();
double distance = Math.pow((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2), 0.5);
float pi = 3.14f;
System.out.println("Perimeter of Circle 1 " + (pi * dia1) );
dia1 = dia1 / 2;
System.out.println("Area of Circle 1 " + (pi * dia1 * dia1));
System.out.println("Perimeter of Circle 1 " + (pi * dia2) );
dia2 = dia2 / 2;
System.out.println("Area of Circle 1 " + (pi * dia2 * dia2));
if (dia2 >= dia1 && distance <= (dia2 - dia1)){
System.out.println("Circle 1 is inside Circle 2.");
}
else if (dia1 >= dia2 && distance <= (dia1 - dia2) ) {
System.out.println("Circle 2 is inside Circle 1.");
}
else if (distance > (dia1 + dia2)){
System.out.println("Circle 2 and Circle 1 does not overlap.");
}
else {
System.out.println("Circle 2 and Circle 1 overlap.");
}
}
else if(sh == 2){
System.out.println("Enter top left corner coordinate of rechtangle 1");
double r1x1 = input.nextDouble();
double r1y1 = input.nextDouble();
System.out.println("enter width and length");
double width1 = input.nextDouble();
double length1 = input.nextDouble();
double r1x2 = r1x1 + width1;
double r1y2 = r1y1 + length1;
Point r1l = new Point(r1x1,r1y1);
Point r1r = new Point(r1x2,r1y2);
System.out.println("Enter top left corner coordinate of rechtangle 2");
double r2x1 = input.nextDouble();
double r2y1 = input.nextDouble();
System.out.println("enter width and length");
double width2 = input.nextDouble();
double length2 = input.nextDouble();
double r2x2 = r2x1 + width2;
double r2y2 = r2y1 + length2;
Point r2l = new Point(r2x1,r2y1);
Point r2r = new Point(r2x2,r2y2);
System.out.println("Perimeter of Rectangle 1 " + (2 * width1 + 2 * length1) );
System.out.println("Area of Rectangle 1 " + (width1 * length1));
System.out.println("Perimeter of Rectangle 2 " + (2 * width2 + 2 * length2) );
System.out.println("Area of Rectangle 2 " + (width2 * length2));
if(doOverlap(r1l, r1r, r2l, r2r))
System.out.println("two rectangle overlaps");
else
System.out.println("Does not overlap");
}else
{
System.out.println("Enter coordinates of line 1");
double l1x1 = input.nextDouble();
double l1y1 = input.nextDouble();
double l1x2 = input.nextDouble();
double l1y2 = input.nextDouble();
System.out.println("Enter coordinates of line 2");
double l2x1 = input.nextDouble();
double l2y1 = input.nextDouble();
double l2x2 = input.nextDouble();
double l2y2 = input.nextDouble();
System.out.println("Length of line 1 " + Math.sqrt(Math.abs(l1x2 - l1x1) * Math.abs(l1x2 - l1x1) + Math.abs(l1y2 - l1y1) * Math.abs(l1y2 - l1y1)) );
System.out.println("Length of line 1 " + Math.sqrt(Math.abs(l2x2 - l2x1) * Math.abs(l2x2 - l2x1) + Math.abs(l2y2 - l2y1) * Math.abs(l2y2 - l2y1)) );
Point lp1 = new Point(l1x1, l1y1);
Point lp2 = new Point(l1x2, l1y2);
Point rp1 = new Point(l2x1, l2y1);
Point rp2 = new Point(l2x2, l2y2);
if(doIntersect(lp1, lp2, rp1, rp2))
System.out.println("Two line intersect each other");
else
System.out.println("two line does not intersect each other");
}
}
static boolean onSegment(Point p, Point q, Point r)
{
if (q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) &&
q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y))
return true;
return false;
}
static int orientation(Point p, Point q, Point r)
{
double val = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (val == 0) return 0;
return (val > 0)? 1: 2;
}
static boolean doIntersect(Point p1, Point q1, Point p2, Point q2)
{
// Find the four orientations needed for general and
// special cases
double o1 = orientation(p1, q1, p2);
double o2 = orientation(p1, q1, q2);
double o3 = orientation(p2, q2, p1);
double o4 = orientation(p2, q2, q1);
// General case
if (o1 != o2 && o3 != o4)
return true;
// Special Cases
if (o1 == 0 && onSegment(p1, p2, q1)) return true;
if (o2 == 0 && onSegment(p1, q2, q1)) return true;
if (o3 == 0 && onSegment(p2, p1, q2)) return true;
if (o4 == 0 && onSegment(p2, q1, q2)) return true;
return false; // Doesn't fall in any of the above cases
}
public static boolean doOverlap(Point l1, Point r1, Point l2, Point r2)
{
if (l1.x > r2.x || l2.x > r1.x)
return false;
if (l1.y < r2.y || l2.y < r1.y)
return false;
return true;
}
}
class Point{
double x;
double y;
public Point(double x, double y){
this.x =x;
this.y = y;
}
}