Working with Negative Infinity (Vertical) slopes in BlueJ - java

Everything in my code finally seems right. i'm just having trouble with something tricky.
How do I write a code so that when i enter two points and the slope is -infinity, it's identified and the output says Vertical and NOT Negative Slope.
For example, a Positive Slope OUTPUT would look like:
Enter the x and y coordinates of the first point: 3 -2
Enter the x and y coordinates of the second point: 9 2
Distance: 7.211
Positive Slope
AND a Vertical would look like:
Enter the x and y coordinates of the first point: 4 5
Enter the x and y coordinates of the second point: 4 -3
Distance: 8.000
Vertical
AND right now my Vertical Output looks like:
Enter the x and y coordinates of the first point: 4 5
Enter the x and y coordinates of the second point: 4 -3
Distance: 8.000
Negative Slope
Here is what my code looks like right now:
import java.util.Scanner;
public class LineEvaluator
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the x and y coordinates of the first point: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print("Enter the x and y coordinates of the second point: ");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
System.out.printf("Distance: %.3f", distance);
double slope = ((y2 - y1) / (x2 - x1));
if(slope > 0)
System.out.println("Positive Slope");
else if(slope < 0)
System.out.println("Negative Slope");
else if(slope == 0)
System.out.println("Horizontal");
}
}

You can check explicitly if slope == Double.NEGATIVE_INFINITY. You should do the same for Double.POSITIVE_INFINITY.
if (slope == Double.NEGATIVE_INFINITY || slope == Double.POSITIVE_INFINITY)
System.out.println("Vertical");
else if(slope > 0)
System.out.println("Positive Slope");
else if(slope < 0)
System.out.println("Negative Slope");
else if(slope == 0)
System.out.println("Horizontal");
Or you can use Double.isInfinite :
if (Double.isInfinite(slope))
System.out.println("Vertical");
else if(slope > 0)
System.out.println("Positive Slope");
else if(slope < 0)
System.out.println("Negative Slope");
else if(slope == 0)
System.out.println("Horizontal");

Related

How to check if a point is located inside a triangle using coordinates?

Suppose a right triangle is placed in a plane as
shown below. The right-angle point is placed at (0, 0), and the other two points
are placed at (200, 0), and (0, 100). Write a program that prompts the user to enter
a point with x- and y-coordinates and determines whether the point is inside the
triangle.
`String xInput, yInput;
double x, y;
xInput = JOptionPane.showInputDialog("Enter the x-coordinate of the point");
yInput = JOptionPane.showInputDialog("Enter the y-coordinate of the point");
x = Double.parseDouble(xInput);
y = Double.parseDouble(yInput);
if (x <= 200 && x >= 0 && y <= 100 && y >= 0) {
if (y = roundup(x/2))
System.out.print("The point is in the the triangle");
else
System.out.print("The point isn't in the triangle");
}else
System.out.print("The point isn't in the triangle");`
The output is an error in the second if saying that a double can't be a boolean
This is the figure for clarifications
Basically you have a linear formula that is y = 100 - x/2 where x is between 0 and 200 so we can create simple method for that
static double calculateY(double x) {
return 100.0 - x / 2.0;
}
and then compare x against the boundaries and y against the formula
if (x < 0 || x > 200 || y < 0) {
System.out.print("The point isn't in the triangle");
} else if ( y <= calculateY(x)) {
System.out.print("The point is in the the triangle");
} else
System.out.print("The point isn't in the triangle");
}
Consider a triangle with vertices at (0, 0), (1, 0), and (0, 1). Write a program that asks the user for x and y coordinates and then outputs whether the point is inside the triangle, on the border of the triangle or outside the triangle.

Distance in BlueJ

OUTPUT should look like:
Enter the x and y coordinates of the first point: 3 -2
Enter the x and y coordinates of the second point: 9 2
Distance: 7.211
Positive Slope
I have updated the OP with what the code should look like if you're trying to find the distance.
Here is what my code looks like right now:
import java.util.Scanner;
public class LineEvaluator
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the x and y coordinates of the first point: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print("Enter the x and y coordinates of the second point: ");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
System.out.printf("Distance: %.3f", distance);
double slope = ((y2 - y1) / (x2 - x1));
if(slope > 0)
System.out.println("Positive Slope");
else if(slope < 0)
System.out.println("Negative Slope");
else if(slope == 0)
System.out.println("Horizontal");
}
}
So... First of all don't say int to double like that - go in the documentations and you'll find that input.nextDouble() exists and you can use that instead directly.
e.x.:
int y2 = input.nextInt();
double g = y2;
to...
double g = input.nextDouble()
Also, in your distance formula, you are using:
double distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2)*(y1 - y2));
If you're using that, please name your doubles to the proper names(x1, x2, etc...)
When you use int, it truncates. Read up on the differences:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Then, you can calculate the slope, say you store it in a variable slope.
When you're trying to find a slope that has infinite slope, you will get this error: ArithmeticException.
You can fix this by a try catch, surrounding the code you might divide by 0, and then System.out.println("Vertical Line") at that situation, or you could evaluate later and leave the try catch blank. Use this: Double.isInfinite(double) to evaluate later on.
Try catch example:
try{
slope = (y2-y1)/(x2-x1)//if it's divide by zero then we will get this error
}catch(ArithmeticException e){
//Take care of the error as we discussed.
}
Use if-elses or switch statements to evaluate the slope:
if(slope > 0)
System.out.println("Positive slope")
else if(slope == 0)
System.out.println("Flat slope")...
Hope you got the idea.

Java - Two Circles overlap or inside

I have a problem on my homework, and I am having trouble getting the last part to work. What am I doing wrong here? It will not print "Circle 2 overlaps Circle 1." or "Circle 2 does not overlap Circle 1." The first two cases do print. Here is the question:
Write a program (TwoCircles.java) that prompts the user to enter the center coordinates and radii of two circles and determines the geometrical relationship between the two and print one of the following messages accordingly:
1. Circle 1 is inside Circle 2.
2. Circle 2 is inside Circle 1.
3. Circle 2 overlaps Circle 1.
4. Circle 2 does not overlap Circle 1.
Here is what i have so far for my code:
import java.util.Scanner;
public class TwoCircles {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter Circle 1 center x-, y-coordinates, and radius: ");
double X1 = input.nextDouble();
double Y1 = input.nextDouble();
double radius1 = input.nextDouble();
System.out.print("Enter Circle 2 center x-, y-coordinates, and radius: ");
double X2 = input.nextDouble();
double Y2 = input.nextDouble();
double radius2 = input.nextDouble();
double distance = Math.pow((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2), 0.5);
if (radius2 >= radius1){
if (distance <= (radius2 - radius1))
System.out.println("Circle 1 is inside Circle 2.");}
else if (radius1 >= radius2){
if (distance <= (radius1 - radius2))
System.out.println("Circle 2 is inside Circle 1.");}
else if (distance > (radius1 + radius2)){
System.out.println("Circle 2 does not overlap Circle 1.");}
else {
System.out.println("Circle 2 overlaps Circle 1.");}
}
}
Any guidance appreciated
Use the following code:
import java.util.Scanner;
public class TwoCircles {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter Circle 1 center x-, y-coordinates, and radius: ");
double X1 = input.nextDouble();
double Y1 = input.nextDouble();
double radius1 = input.nextDouble();
System.out.print("Enter Circle 2 center x-, y-coordinates, and radius: ");
double X2 = input.nextDouble();
double Y2 = input.nextDouble();
double radius2 = input.nextDouble();
double distance = Math.pow((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2), 0.5);
if (radius2 >= radius1 && distance <= (radius2 - radius1)){
System.out.println("Circle 1 is inside Circle 2.");
}
else if (radius1 >= radius2 && distance <= (radius1 - radius2) ) {
System.out.println("Circle 2 is inside Circle 1.");
}
else if (distance > (radius1 + radius2)){
System.out.println("Circle 2 does not overlap Circle 1.");
}
else {
System.out.println("Circle 2 overlaps Circle 1.");}
}
}
The problem with the code which you are using is that either of the first two conditions:
if(radius1>=radius2)
or
else if(radius1<=radius2)
will always be true and hence the code will never reach the third or fourth condition. The correct way to achieve what you are trying to do is to merge all the conditions together which i have done in the code presented above.
Hope this helps!
You probably "lost" yourself in that if-else statements.
First of all, I would recommend to write it with ALL the braces to understand correctly, what exactly is happening there :
if (radius2 >= radius1) {
if (distance <= (radius2 - radius1)) {
System.out.println("Circle 1 is inside Circle 2.");
}
} else if (radius1 >= radius2) {
if (distance <= (radius1 - radius2)) {
System.out.println("Circle 2 is inside Circle 1.");
}
} else if (distance > (radius1 + radius2)) {
System.out.println("Circle 2 does not overlap Circle 1.");
} else {
System.out.println("Circle 2 overlaps Circle 1.");
}
As you can see, you say "if radius2 is equal or greater than radius1" do something and if not and if "radius1 is greater or equal radius2" do something else.
If you think about it, there is no possibility which is neither first or second condition. So the last two statements are logically unreachable.

Finding whether a point is within a triangle

I have been on this for hours, attempting different methods looking at just about every question. Perhaps I have it completely wrong, but I feel that I have my math of it correct, but no matter what numbers I input, I get the same output. My code is off somewhere and I have to turn it in by midnight.
It is the all so fun: Find if a point is within a triangle code. (for beginners)
import java.util.Scanner;
public class PointsTriangle {
// checks if point entered is within the triangle
//given points of triangle are (0,0) (0,100) (200,0)
public static void main (String [] args) {
//obtain point (x,y) from user
System.out.print("Enter a point's x- and y-coordinates: ");
Scanner input = new Scanner(System.in);
double x = input.nextDouble();
double y = input.nextDouble();
//find area of triangle with given points
double ABC = ((0*(100-0 )+0*(0 -0)+200*(0-100))/2.0);
double PAB = ((x*(0 -100)+0*(100-y)+0 *(y- 0))/2.0);
double PBC = ((x*(100-0 )+0*(0 -y)+200*(y-100))/2.0);
double PAC = ((x*(0 -100)+0*(100-y)+200*(y- 0))/2.0);
boolean isInTriangle = PAB + PBC + PAC == ABC;
if (isInTriangle)
System.out.println("The point is in the triangle");
else
System.out.println("The point is not in the triangle");
}//end main
}//end PointsTriangle
You placed the wrong value order into your formula; therefore, the result is wrong. If the 3 vertices are as the following
A(x1, y1) B(x2, y2), C(x3, y3)
then the area is calculated as
double ABC = Math.abs (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2;
After that, you just replace each vertex with the input point, we will have the following triangles: PBC, APC, ABP.
Put everything together, we will have the correct one
int x1 = 0, y1 = 0;
int x2 = 0, y2 = 100;
int x3 = 200, y3 = 0;
// no need to divide by 2.0 here, since it is not necessary in the equation
double ABC = Math.abs (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
double ABP = Math.abs (x1 * (y2 - y) + x2 * (y - y1) + x * (y1 - y2));
double APC = Math.abs (x1 * (y - y3) + x * (y3 - y1) + x3 * (y1 - y));
double PBC = Math.abs (x * (y2 - y3) + x2 * (y3 - y) + x3 * (y - y2));
boolean isInTriangle = ABP + APC + PBC == ABC;
If you draw a picture, you can see the point has to satisfy simple inequalities (below / above / to the right of certain lines). Whether "on the edge" is in or out I will leave up to you:
Y > 0 (above the X axis)
X > 0 (to the right of the Y axis)
X + 2* Y < 200 (below the hypotenuse)
Write an if statement around these three and you're done:
if( (y > 0) && (x > 0) && (x + 2*y < 200) )
System.out.println("The point is in the triangle");
else
System.out.println("The point is not in the triangle");

Java 2d Shooting direction with mouse

My problem might be simple. I've spent most of today thinking of an algorithm(That will pretty probably be a couple if statements) that will determine the direction the mouse is pointing and shoot a bullet in that direction. I already tested the bullets to make sure they shoot by shooting in a defined direction.
how would i go about calculating if the mouse is on the left side of the player, the right side,top side, bottom side, or if it's on the corners of the player ?
Solved: Thanks for all your help but after a day of thinking I came up with a way my self. What I did is use the if statements to determine when I press the mouse down, is it going to be colliding with the top part of the player,bottom,right,left,or corners. Anyway, here is my code. P.S. I used the variable x1 as the mousex, y1 as mousey, x as playerx, and y as player y. The only other variable I have is dx and dy but you should know what those do.
//top
if (x1 > x && x1 < x + 40 && y1 > y - 250 && y1 < y){
dy = -1;
dx = 0;
}
//right
if (x1 > x + 40 && x1 < x + 250 && y1 > y && y1 < y + 40){
dx = 1;
dy = 0;
}
//bottom
if (x1 > x && x1 < x + 40 && y1 > y+40 && y1 < y+250){
dy = 1;
dx = 0;
}
//left
if (x1 < x && x1 > x - 250 && y1 > y && y1 < y + 40){
dx = -1;
dy = 0;
}
//top right corner
if (x1 > x + 40 && x1 < x + 250 && y1 > y - 250 && y1 < y){
dx = 1;
dy = -1;
}
//top left corner
if (x1 < x && x1 > x - 250 && y1 > y - 250 && y1 < y){
dx = -1;
dy = -1;
}
//bottom right corner
if (x1 > x + 40 && x1 < x + 250 && y1 > y + 40 && y1 < y + 250){
dx = 1;
dy = 1;
}
//bottom left corner
if (x1 < x && x1 > x - 250 && y1 > y + 40 && y1 < y + 250){
dx = -1;
dy = 1;
}
You have to implement Mouse move actionlistener if you want to implement shooting while the mouse button is pressed.
Simple equation of line will do it.
Solution: Get the initial point (x0,y0) when button is pressed. While mouse moves when pressed, get (x1,y1) point where the mouse is moving (this constantly changes) - get the line equation - (you have 2 points so find slope and then use one point to get the equation of the line).
Now the direction the bullet fires is the perpendicular to this line through (x1,y1). So you can find the equation of this perpendicular line when the other equation is known. Now to know whether it has to be fired up or down is relative to finding out which side the gun is pointed (direction be stored in a variable)
After all this, when mouse still moves, old point will now be (x1,y1) and new point will be (x2,y2) and you keep implementing these changes.

Categories

Resources