(java) Decide if a line intercepts a circle - java

So, I need to write a program that decides whether a line intercepts a circle or not. Specific interception coordinates are not required, and simply being touching the circle or being tangent to it causes interception as well. Apologies if this has been asked before, but I couldn't find anything that suited my problem. Maybe I didn't look hard enough.
import java.util.Scanner;
public class LineCircle_Intersection {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double p1x, p2x, p1y, p2y, cx, cy, r;
System.out.print("Enter p1x: ");
p1x = in.nextDouble();
System.out.print("Enter p1y: ");
p1y = in.nextDouble();
System.out.print("Enter p2x: ");
p2x = in.nextDouble();
System.out.print("Enter p2y: ");
p2y = in.nextDouble();
System.out.print("Enter cx: ");
cx = in.nextDouble();
System.out.print("Enter cy: ");
cy = in.nextDouble();
System.out.print("Enter r: ");
r = in.nextDouble();
if ((p1x - cx < r) && (p1y - cy < r))
System.out.println("The line intersects the circle.");
else if ((p2x - cx < r) && (p2y - cy < r))
System.out.println("The line intersects the circle.");
else
System.out.println("The line does not intersect the circle.");
}
}
I've been working on an alternate route that uses line and circle formulas instead of the input points, but it's kind of a mess and I'm not sure where I'm going with it.

We can write the equation for the line as y-p1y = m (x-p1x), where m = (p2y-p1y)/(p2x-p1x)
then y = m(x-p1x) + p1y (1)
We can write the equation for the circle as (x-cx)^2 + (y-cy)^2 = r^2 (2)
If we plug (1) into (2) we get:
x^2 (1+m) + x (-2a+2cm-2dm-2bm) + a^2-2cmb+2dmb+mb^2+c^2-2cd+d^2-r^2=0
where
a = cx
b = p1x
c = p1y
d = cy
let
a' = (1+m)
b' =(-2a+2cm-2dm-2bm)
c' = a^2-2cmb+2dmb+mb^2+c^2-2cd+d^2-r^2
we know then that the line and the circurfence touch each other if
b'^2-4a'c' >= 0

Related

Java geometry problem: perimeter of circular segment

Seq is a method which for given line: line's point a and slope b returns perimeter of circular segment. I used the equation of circle and line and solved it for x, and then I easily get two points. But instead of (3.5,1.9) approximately I get (6.528,0.0832000000000006)..
public class Circle {
Point center;
double r;
public double seq(Point a, double b) {
double n = -b*a.getX() + a.getY();
System.out.println("n: "+n);
if (r*r*(b*b+1)-Math.pow(b*center.getX()-center.getY()+n, 2) > 0) {
double temp = (1+b*b);
System.out.println("coeficient x^2: "+temp);
double temp1 = 2*((-1)*center.getX()+b*n-b*center.getY());
System.out.println("coeficient x: "+ temp);
double free_term = center.getX()*center.getX() + n*n -2*n*center.getY() + center.getY()*center.getY() - r*r;
System.out.println("free term: "+free_term);
double D = Math.sqrt(temp1*temp1-4*temp*free_term);
System.out.println(temp1*temp1-4*temp*free_term);
double x1 = ((-1)*temp1+Math.sqrt(temp1*temp1-4*temp*free_term))/2*temp;
double x2 = ((-1)*temp1-Math.sqrt(temp1*temp1-4*temp*free_term))/2*temp;
double y1 = b*x1 - b*a.getX() + a.getY();
double y2 = b*x2 - b*a.getX() + a.getY();
System.out.println("("+x1+","+y1+")"+", ("+x2+","+y2+")");
Point A = new Point(x1,y1);
Point B = new Point(x2,y2);
double d = A.dist(B);
System.out.println("d: "+d);
double angle = Math.acos((2*r*r-d*d)/2*r*r);
System.out.println("angle "+ alfa);
double l = r*Math.PI*angle/Math.toRadians(180);
return l+d;
} else return 0;
}
}
Main:
Circle k = new Circle();
Point c = new Point(0,0);
k.center = c;
k.r = 4;
Point a = new Point(0,4);
System.out.println(k.seq(a, -3.0/5.0));
Console:
n: 4.0
coeficient x^2: 1.3599999999999999
coeficient x: -4.8
free term: 0.0
23.04
(6.528,0.0832000000000006), (0.0,4.0)
d: 7.612890793910023
angleNaN
NaN
Your problem is in the implementation of the quadratic formula. You might think that A/2*B means A/(2*B) but in reality it's (A/2)*B. So add the parentheses in around the divisor:
double x1 = ((-1)*temp1+Math.sqrt(temp1*temp1-4*temp*free_term))/(2*temp);
double x2 = ((-1)*temp1-Math.sqrt(temp1*temp1-4*temp*free_term))/(2*temp);

Finding The Area Of A Triangle

I have a question that I would like to ask you. In my book for java programming, it asks me to write a program that finds the area of a triangle given 3 points. I tried many ways but I could never get the right answer. Can you please give me a solution to this problem. Thanks! Here is the question:
Here is my code:
import java.util.Scanner;
public class shw2point15 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter three points for a triangle:");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double x3 = input.nextDouble();
double y3 = input.nextDouble();
double s = ((x1 + y1) + (x2 + y2) + (x3 + y3)) / 2;
double area = Math.sqrt(s * (s - (x1 - y1)) * (s - (x2 - y2)) * (s - (x3 - y3)));
System.out.println("The area of the triangle is " + area);
}
}
The reason you're not getting a correct answer is because you are not finding the sides correctly. However, after finding the side length you can get the answer. Here is what I did:
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter three points for a triangle:");
//Store the values in an array.
double[] xCoordinates = new double[3];
double[] yCoordinates = new double[3];
double[] sides = new double[3];
// Input the values into the array
xCoordinates[0] = input.nextDouble();
yCoordinates[0] = input.nextDouble();
xCoordinates[1] = input.nextDouble();
yCoordinates[1] = input.nextDouble();
xCoordinates[2] = input.nextDouble();
yCoordinates[2] = input.nextDouble();
// Find the side length from the input. There probably are better ways to do this.
sides[0] = Math.sqrt(Math.pow(xCoordinates[0]-xCoordinates[1], 2)+Math.pow(yCoordinates[0]-yCoordinates[1], 2));
sides[1] = Math.sqrt(Math.pow(xCoordinates[1]-xCoordinates[2], 2)+Math.pow(yCoordinates[1]-yCoordinates[2], 2));
sides[2] = Math.sqrt(Math.pow(xCoordinates[2]-xCoordinates[0], 2)+Math.pow(yCoordinates[2]-yCoordinates[0], 2));
// Find s from the sides
double s = ( sides[0]+sides[1]+sides[2] )/2;
// Find the area.
double area = Math.sqrt(s*( s-sides[0] )*( s-sides[1] )*( s-sides[2] ));
// Print the area
System.out.println("The area of the triangle is "+area);
// Output~~~~~~~~~~~~~~~
//Enter three points for a triangle:
// 1.5
// -3.4
// 4.6
// 5
// 9.5
// -3.4
// The area of the triangle is 33.600000000000016
}'

Calculating the area of a triangle

I am new to Java. I am trying to calculate the area of a triangle using the formula:
s = (side 1 + side 2 + side 3)/2
area = square root (side (side - side 1)(side - side2)(side - side3).
If the user enter the three point as:
1.5 -3.4 4.6 5 9.5 -3.4 then the area of the triangle should be 33.6. However, my program runs, but it's giving me an incorrect answer. Below is my code.
// Import Java Scanner
import java.util.Scanner;
import java.lang.Math;
public class Ex_2_19 {
public static void main(String[] args) {
//Create a Scanner object
Scanner input = new Scanner(System.in);
float side = 0;
float area1 = 0;
float area2 = 0;
float area3 = 0;
float area4 = 0;
float calculatedarea = 0;
//Prompt the user to enter three points of a triangle
System.out.println("Enter point x1:");
System.out.println("Enter point y1:");
System.out.println("Enter point x2:");
System.out.println("Enter point y2:");
System.out.println("Enter point x3:");
System.out.println("Enter point y3:");
//Define the variables
float Pointx1 = input.nextFloat();
float Pointy1 = input.nextFloat();
float Pointx2 = input.nextFloat();
float Pointy2 = input.nextFloat();
float Pointx3 = input.nextFloat();
float Pointy3 = input.nextFloat();
//Formula to calculate the area of a triangle
side = (Pointx1 + Pointy1 + Pointx2 + Pointy2 + Pointx3 + Pointy3) / 2;
area1 = side - (Pointx1 + Pointy1);
area2 = side - (Pointx2 + Pointy2);
area3 = side - (Pointx3 + Pointy3);
area4 = side * area1 * (area2) * (area3);
calculatedarea = (float) (Math.sqrt(area4));
//calculatedarea = (float) (Math.sqrt(area1)*(area2) * (area3));
//Print result
System.out.println("The area of the triangle is " + calculatedarea);
}
}
You are trying Heron's Formula - note that a, b, c are the euclidean distance between the points, thus will need to be computed by sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)), etc., not just the sum of differences.

Everything I do is either over-complicated or wrong

// *************************************************************
// Distance.java
// Computes the distance between two points in java
// *************************************************************
import java.util.Scanner;
import java.lang.Math;
public class Distance
{
public static void main (String[] args)
{
double x1, y1, x2, y2; // coordinates of two points
double distance; // distance between the points
//Formula - D=√(x2-x1)^2 + (y2-y1)^2
Scanner scan = new Scanner(System.in);
// Read in the two points
System.out.print ("Enter the coordinates of the first point " +
"(put a space between them): ");
x1 = scan.nextDouble();
y1 = scan.nextDouble();
System.out.print ("Enter the coordinates of the second point: ");
x2 = scan.nextDouble();
y2 = scan.nextDouble();
// Compute the distance
double math = x1-x2 * y1-y2;
// Print out the answer
System.out.print("The distance between (" + x1 + "," + x2 + ") and (" + y1 + "," + y2 + ") is") ;
}
}
I keep running into errors and I go on forever with codes that can be done faster and shorter. All I need to do is read the user input for coordinates, find the distance between them, and print the answer.
First you create your Point Class. Remember this is Java and OOP
class Point {
private double x;
private double y;
private String name;
public Point(String n, double xx, double yy) {
this.name = n;
this.x = xx;
this.y = yy;
}
public double getX() {
return this.x;
}
public double getY() {
return this.y;
}
public String getName() {
return this.name;
}
}
Secondly you create your Distance class with the distance calculating method
public double getEuclideanDistance(Point p, Point q) {
double ed = Math.sqrt((q.getX() - p.getX()) * (q.getX() - p.getX()) + (q.getY() - p.getY()) * (q.getY() - p.getY()));
return ed;
}
Lastly modify you main method as below
public static void main(String[] args) {
double Xp;
double Yp;
double Xq;
double Yq;
Scanner reader = new Scanner(System.in);
System.out.print("Enter x- cordinate of P : ");
Xp = reader.nextDouble();
System.out.print("Enter y- cordinate of P : ");
Yp = reader.nextDouble();
System.out.print("Enter x- cordinate of Q : ");
Xq = reader.nextDouble();
System.out.print("Enter y- cordinate of Q : ");
Yq = reader.nextDouble();
Point q = new Point("Q", Xq, Yq);
Point p = new Point("P", Xp, Yp);
System.out.println("Point " + p.getName() + "'s Coordinates are: " + "P(" + p.getX() + " , " + p.getY() + ")");
System.out.println("Point " + q.getName() + "'s Coordinates are: " + "Q(" + q.getX() + " , " + q.getY() + ")");
System.out.println("The Euclidean distance from p to q is :" + getEuclideanDistance(p, q));
}
The issue is that you are not quite implementing the distance formula the correct way... you have double math = x1-x2 * y1-y2 but aren't calling Math.pow() or Math.sqrt() at all.
Try to implement the following:

Loop will not end. How do i make it exit?

Got scolded earlier for not specify the question. Promise to do better this time.
For some reason, the loop doesn't stop when i enter 0, instead it continues to print out the main method again. How do i fix this? I couldn't find out what is wrong with it, since everything else runs smoothly.
Also, I want to use Switch instead of If, but i keep getting Duplicate Variable error. Why?
import java.util.Scanner;
public class FindArea
{
// side is only input provided to cube. Area of the cube is returned
public static double cube (double side)
{
double Area;
return Area = 6 * side * side;
}
// radius is only input provided to sphere. Area of the sphere is returned
public static double sphere (double radius)
{
double Area;
return Area = 4 * 3.14 * radius * radius;
}
// radius and height are the only inputs provided to cylinder.
// Area of the cylinder is returned
public static double cylinder (double radius, double height)
{
double Area;
return Area = 2 * 3.14 * radius * height + 2 * 3.14 * radius * radius;
}
// outerR and innerR are the only inputs provided to doughnut.
// Area of the doughnut is returned
public static double doughnut (double outerR, double innerR)
{
double Area;
return Area = (2 * 3.14 * innerR) * (2 * 3.14 * outerR);
}
public static void main(String[] args)
{
int n = 4;
Scanner input = new Scanner(System.in);
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
do
{
n = input.nextInt();
if ( n<0 || n>4) {
System.out.println("Invalid");
}
if(n == 1) {
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
}
if (n == 2) {
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
}
if (n == 3) {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
}
if (n == 4) {
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
}
System.out.println("--------");
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
} while (n != 0);
System.exit(1);
}
}
It prints the output one more time because the comparison is not made until the end of the Do-While loop.
It checks for all cases of n other than 0, and prints the output no matter what.
I think what you're looking for is more so:
n = input.nextInt();
if(n!=0){
if ( n<0 || n>4) {
System.out.println("Invalid");
}
if(n == 1) {
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
}
if (n == 2) {
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
}
if (n == 3) {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
}
if (n == 4) {
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
}
System.out.println("--------");
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
}
note the n!=0
Additionally look into the use of switch-case, once you learn them these comparisons will not only be faster, but very easy to comprehend
You can try this with your switch case -
public static void main(String[] args)
{
int n = 4;
Scanner input = new Scanner(System.in);
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
n = input.nextInt();
if ( n<0 || n>4) {
System.out.println("Invalid");
}
else{
switch(n){
case 0:{
System.out.println("You enter 0 to exit");
break;
}
case 1:{
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
break;
}
case 2:{
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
break;
}
case 3: {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
break;
}
case 4:{
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
break;
}
}
}
}
i have done this using switch case. Very simple as well
public class FindArea {
// side is only input provided to cube. Area of the cube is returned
public static double cube(double side) {
double Area;
return Area = 6 * side * side;
}
// radius is only input provided to sphere. Area of the sphere is returned
public static double sphere(double radius) {
double Area;
return Area = 4 * 3.14 * radius * radius;
}
// radius and height are the only inputs provided to cylinder.
// Area of the cylinder is returned
public static double cylinder(double radius, double height) {
double Area;
return Area = 2 * 3.14 * radius * height + 2 * 3.14 * radius * radius;
}
// outerR and innerR are the only inputs provided to doughnut.
// Area of the doughnut is returned
public static double doughnut(double outerR, double innerR) {
double Area;
return Area = (2 * 3.14 * innerR) * (2 * 3.14 * outerR);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out
.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut, Or enter 0 to exit ");
int n = 0;
double side = 0;
double Area = 0;
double radius = 0;
double height = 0;
n = input.nextInt();
switch (n) {
case 0:
System.out.println("Exit");
System.exit(1);
break;
case 1:
System.out.print("Enter side measurement of cube: ");
side = input.nextDouble();
Area = cube(side);
System.out.println("The area of the cube is: " + Area);
break;
case 2:
System.out.print("Enter radius measurement of sphere: ");
radius = input.nextDouble();
Area = sphere(radius);
System.out.println("The area of the sphere is: " + Area);
break;
case 3:
System.out.print("Enter radius measurement of cylinder: ");
radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
height = input.nextDouble();
Area = cylinder(radius, height);
System.out.println("The area of the cylinder is: " + Area);
break;
case 4:
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
Area = doughnut(outerR, innerR);
System.out.println("The area of the donut is: " + Area);
break;
default:
System.out.println("--------");
System.out.println("Invalid option selected");
System.out.println("--------");
System.out
.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
n = input.nextInt();
break;
}
}
}
Good Luck.

Categories