Question

my question is on a java program. i have added all instructions for this homeowrk assignment....

my question is on a java program. i have added all instructions for this homeowrk assignment. from how the input is entered and what the output should read. im seeing if i can get a psuedo code setup for how the algorithm should be setup.

Build an Intersection Check program. User inputs two points as x-y coordinates in twodimension space. Two points represent two end points of a line segment. Then, user can input second two points for the second line segment. With the two line segments, the program checks whether or not the two line segments intersect. The program displays “1” if the two line segments are crossing. Otherwise, display “0”. Please implement your algorithm in Java but not use API. Hint: use slop and intercept

Input: Four x-y coordinates that represent two point for the first line segment and two points for the second line segments

Output: 0 or 1 that represents intersection or no intersection

Example Input: 7 8 5 12 10 10 3 10

Output: 1 Do you want to test again? Y

Input: 5 10 5 2 2 1 8 7 Output: 1

Do you want to test again? Y

Input: 10 10 3 10 6 3 6 6 Output: 0

Do you want to test again? Y

Input: 5 4 8 6 10 6 5 1 Output: 0

Do you want to test again? N

Thank you!

need help with this java program. any help out there

0 0
Add a comment Improve this question Transcribed image text
Answer #1
/**
 * @fileName LineIntersect.java
 * @author 
 * @since 4/2/17
 */


package lineintersect;

import java.util.Scanner;

public class LineIntersect {

    static Point p = new Point();

    /**
     * Calculating distace Between two points
     * @param a Point
     * @param b Point
     * @return  distance
     */
    public static double distance(Point a, Point b) {
        return Math.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
    }

    /**
     *This method will tell point c is in between a and b or not
     * @param a Point
     * @param b Point
     * @param c Point
     * @return True/False
     */
    public static boolean isBetween(Point a, Point b, Point c) {
        return distance(a, c) + distance(c, b) == distance(a, b);
    }

    public static void intersectionWithVerticalLine(Line l, float x) {
        p.y = l.m * x + l.c;
        p.x = x;
    }

    /**
     * This function finds the intersection point of a line when m!=infinite
     * @param l1 First line
     * @param l2 Second Line
     */
    public static void normalIntersection(Line l1, Line l2) {

        float x = (l2.c - l1.c) / (l1.m - l2.m);
        float y = l1.c + ((l2.c - l1.c) * l1.m / (l1.m - l2.m));
        p.x = x;
        p.y = y;

    }


    public static int getIntersectionPoint(Line l1, Line l2) {

        if (l1.p1.x == l1.p2.x) {
            intersectionWithVerticalLine(l2, l1.p1.x);
        } else if (l2.p1.x == l2.p2.x) {
            intersectionWithVerticalLine(l1, l2.p1.x);
        } else {
            normalIntersection(l1, l2);
        }
        if (isBetween(l1.p1, l1.p2, p) && isBetween(l2.p1, l2.p2, p)) {

            return 1;
        } else {
            return 0;
        }

    }


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("Enter the Points");
            System.out.print("Input:");
            float x1 = sc.nextFloat();
            float y1 = sc.nextFloat();
            float x2 = sc.nextFloat();
            float y2 = sc.nextFloat();
            float x3 = sc.nextFloat();
            float y3 = sc.nextFloat();
            float x4 = sc.nextFloat();
            float y4 = sc.nextFloat();

            Point p1 = new Point(x1, y1);
            Point p2 = new Point(x2, y2);
            Point p3 = new Point(x3, y3);
            Point p4 = new Point(x4, y4);

            Line l1 = new Line(p1, p2);
            Line l2 = new Line(p3, p4);
            System.out.print ("  Output:" + LineIntersect.getIntersectionPoint(l1, l2));
            System.out.println();
            System.out.println("Do you want to test again?");
            char s = sc.next().charAt(0);
            if (s == 'N' || s == 'n') {
                break;
            }


        }

    }

}

class Line {

    Point p1;
    Point p2;
    float m;
    float c;

    /**
     * This is parametrized constructor to initialize a Line instance
     * @param p1 Point
     * @param p2 Point
     */
    public Line(Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
        getSlop();
        getIntercept();
    }


    /**
     *calculating the slop m  for this line
     */
    private void getSlop() {
        if (p2.y == p1.y) { //if y2-y1 ==0 menas slop is  0  line is a horizontal line
            this.m = 0;
        } else if (p1.x != p2.x) {
            this.m = (p2.y - p1.y) / (p2.x - p1.x);  // calculating sloap using formula (y2-y1)/(x2-x1)
        }
    }

    private void getIntercept() {


        //if  x1 and x2 are equals then slop will be infinite so then we line of  equation  will be x= some value
        if (p1.x != p2.x) {

            //if slop of line  m is 0 then  intercept will be y1 or y2 because they  are equal thats why m is 0
            if (m == 0) {
                this.c = p1.y;

            } else {

                //calculating intercept by putting x y and m value in equation of line  y=mx +c => c= y-mx
                this.c = p1.y - m * p1.x;

            }
        }
    }

    @Override
    public String toString() {
        return "Line{" +
                "y=" + m + "x +" + c + '}' + p1.toString() + " " + p2.toString();
    }
}

class Point {
    float x;
    float y;

    /**
     * This is parametrized constructor which initialize Point
     * @param x
     * @param y
     */
    public Point(float x, float y) {
        this.x = x;
        this.y = y;
    }

    /**
     * Default constructor
     */
    public Point() {

    }

    @Override
    public String toString() {
        return "Point{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }
}

output:

Enter the Points Input:7 8 5 12 10 10 3 10 Output:1 Do you want to test again? Enter the Points Input:5 10 5 2 2187 Output:1

Add a comment
Know the answer?
Add Answer to:
my question is on a java program. i have added all instructions for this homeowrk assignment....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java program: Convex Hull using Divide and Conquer Algorithm A convex hull is the smallest convex...

    Java program: Convex Hull using Divide and Conquer Algorithm A convex hull is the smallest convex polygon containing all the given points. Input is an array of points specified by their x and y coordinates. The output is the convex hull of this set of points. Examples: Input : points[] = {(0, 0), (0, 4), (-4, 0), (5, 0), (0, -6), (1, 0)}; Output : (-4, 0), (5, 0), (0, -6), (0, 4) use Divide and Conquer Algorithm please explain...

  • Need help with java programming. Here is what I need to do: Write a Java program...

    Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1.     The input and variable value file are both text files that will be specified in...

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and...

    IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. For example if file input is: This is a test File output should be 1. This is a test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BELOW FROM NOTEPAD FILE. This file contains lines of text to determine if you can properly read and write from a file.

  • package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task...

    package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task #1 before adding Task#2 where indicated. */ public class NumericTypesOriginal { public static void main (String [] args) { //TASK #2 Create a Scanner object here //identifier declarations final int NUMBER = 2 ; // number of scores int score1 = 100; // first test score int score2 = 95; // second test score final int BOILING_IN_F = 212; // boiling temperature double fToC;...

  • In this assignment, you will write a Java program(s) to print the binary representation of a...

    In this assignment, you will write a Java program(s) to print the binary representation of a positive integer inserted from command line.  You must finish your assignment in 2 different ways: Using a recursive method Using an iterative method     Your main method must be: public static void main(String[] args) {      int input;         input = Integer.parseInt(args[0]);     print_recursion(input);     print_binary(input); }     You must implement a class and test your program by different input values, including 0. Comment your program properly Example of test...

  • Must be in Java. Show proper reasoning with step by step process. Comment on the code...

    Must be in Java. Show proper reasoning with step by step process. Comment on the code please and show proper indentation. Use simplicity. Must match the exact Output. CODE GIVEN: LineSegment.java LineSegmentTest.java Point.java public class Point { private double x, y; // x and y coordinates of point /** * Creates an instance of Point with the provided coordinates * @param inX the x coordinate * @param inY the y coordinate */ public Point (double inX, double inY) { this.x...

  • my subject :Introduction to Computer Graphics Problem #1 You need to write a program which implements...

    my subject :Introduction to Computer Graphics Problem #1 You need to write a program which implements both DDA and Bresenham’s Line Generation Algorithms. The program should prompt the user to enter tow points coordinate and then compute and display the estimated coordinate of the line segment on the screen. Example Please enter your line segment starting point? 10 10 Please enter the coordinate of the line segment end point? 50 30 What algorithm to use DDA Bresenham’s Your program should...

  • For Java Write a program that reads a sequence of integer inputs all on one line...

    For Java Write a program that reads a sequence of integer inputs all on one line and prints: 1.)The smallest and largest of the inputs 2.)The number of odd and even inputs 3.)Cumulative totals. If the input is 1 7 2 9, the program prints 1 8 10 19. 4.)All adjacent duplicates. If the input is 1 3 3 4 5 5 6 6 6 2, the output is 3 5 6.

  • Create a new class MathOP2 (MathOP2.java) that Inherits from MathOP You need to add multiply method...

    Create a new class MathOP2 (MathOP2.java) that Inherits from MathOP You need to add multiply method and divide method, both methods accepts two parameters and return a value. Create a test program with documentation to test all operators (at least 4) The TestMathOP should do the following      Enter the First number >> 5.5      Enter the Second Number >> 7.5      The sum of the numbers is 13      The subtract of the two numbers is -2.00      Do...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT