Q: How many intersect points does n line have? The question is, given n lines by giving x0, y0, x1, y1. Get the number of meeting points. there will be no point that three or more lines intersect. Lines pass (x0, y0), (x1, y1). There will be no same line.
I have an idea is first declare a HashMap, when a new line come, get its slop. If the slop value is not in the HashMap, the result = result + how many lines we have right now. If the slop is in the HashMap, the result = result + (how many lines we have right now - how many lines is with this slop).
Here is my main part of code.
public static void main(String[] args){
    Scanner in = new Scanner(System.in);
    int[][] lines = new int[in.nextInt()][4];
    Map<Double, Integer> map = new HashMap<>();
    int cross = 0;
    for(int i = 0; i< lines.length; i++){
        for(int j = 0; j< 4; j++)
            lines[i][j] = in.nextInt();
        double slop;
        if(lines[i][2] - lines[i][0] == 0)
            slop = Double.POSITIVE_INFINITY;
        else
            slop = (double)(lines[i][3] - lines[i][1]) / (lines[i][2] - lines[i][0]);
        if(!map.containsKey(slop)) {
            cross = cross + i;
            map.put(slop, 1);
        }else{
            cross = cross + (i - map.get(slop));
            map.put(slop, map.get(slop) + 1);
        }
    }
    System.out.print(cross + "\n");
}

But the test results shows it is wrong. Could someone help me with which case that I didnt notice or is there anything wrong with my code.

A: Any pair of non-parallel lines will intersect.
Therefore, the number of intersections is a polynomial function of the number of unique slopes.
The slope of a line is given as Rise/Run, but it's dangerous to use this division directly since in floating-point mathematics 4.2/2.2 and 8.4/4.4 may not give the same result. Similarly, subtracting x0-x1 may introduce floating-point weirdness.
Since it looks as though you are reading the coordinates as integers, consider storing your slopes as (Rise,Run) pairs. But notice that -Rise/Run and Rise/-Run look different; therefore, make a convention that if a slope is negative, that sign should be stored in the Rise. Now, simplify your fraction, perhaps by using a GCD algorithm.
Now you have slope in a unique and exact form (much better than what you had before).
Now, proceed as before: if the slope is unique then the number of slopes increases by the number of entries already in your hashset.

A: This is a nice little problem. Since we are dealing with lines, and we know that the same line doesn't appear twice, and we know that the same intersection point doesn't appear twice, the solution is almost as simple as counting one intersection point per pair of lines, i.e. n * (n-1) / 2. However, this assumes every pair of lines has an intersection point, which is not true if any lines are parallel; so we need to count how many lines share the same slope, for each slope. It looks like you've figured that out already.
However, it's not clear from your code that you're using those slope counts correctly to calculate the number of intersections. There's some addition and subtraction inside your loop which is hard to reason about. The algorithm is much cleaner if you do it in two separate stages.
If there are r lines with the same slope, then the n * (n-1) / 2 formula assumes they would make r * (r-1) / 2 intersection points, but since the lines are parallel they actually make 0 intersection points between them. So we need to subtract r * (r-1) / 2 to correct the count. We need to do this for each slope independently.
So the algorithm can work as follows:


*

*Build a Map of slopes to counts.

*Initialise a total to n * (n-1) / 2 where n is the number of lines.

*For each count r in the Map's values, subtract r * (r-1) / 2 from the total.


Here's an implementation:
import java.util.Map;
import java.util.HashMap;
import java.util.Scanner;

public class CountIntersections {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long n = in.nextInt();

        Map<Double, Integer> slopeCounts = new HashMap<>();
        for(int i = 0; i < n; i++) {
            int x0 = in.nextInt(), y0 = in.nextInt(),
                x1 = in.nextInt(), y1 = in.nextInt();

            double slope;
            if(x0 == x1) {
                slope = Double.POSITIVE_INFINITY;
            } else {
                slope = (double) (y1 - y0) / (x1 - x0);
            }

            slopeCounts.put(slope, slopeCounts.getOrDefault(slope, 0) + 1);
        }

        long total = n * (n-1) / 2;
        for(long r : slopeCounts.values()) {
            total -= r * (r-1) / 2;
        }
        System.out.println(total);
    }
}

The variables n, total and r are declared as type long instead of int so that the multiplication n * (n-1) doesn't overflow. This is an issue for values of n and/or r greater than about 46,000.
Note that there's no problem with calculating the slopes as doubles here. The numbers being read from the Scanner are ints, all of which are exactly representable as doubles. The subtractions will be exact, and the IEEE 754 specification guarantees that division of floating point numbers will be correctly rounded. Therefore numerical imprecision will not cause two equal slopes to be calculated slightly differently due to floating point arithmetic.
