Search in sources :

Example 1 with Interval1D

use of edu.princeton.cs.algs4.Interval1D in project algorithms-sedgewick-wayne by reneargento.

the class Exercise2 method getIntervals.

private static void getIntervals(Interval1D[] intervals) {
    for (int i = 0; i < intervals.length; i++) {
        double firstValue = StdRandom.uniform();
        double secondValue = StdRandom.uniform();
        if (firstValue > secondValue) {
            double temp = firstValue;
            firstValue = secondValue;
            secondValue = temp;
        }
        intervals[i] = new Interval1D(firstValue, secondValue);
    }
}
Also used : Interval1D(edu.princeton.cs.algs4.Interval1D)

Example 2 with Interval1D

use of edu.princeton.cs.algs4.Interval1D in project algorithms-sedgewick-wayne by reneargento.

the class Exercise3 method generateInterval1D.

private static Interval1D generateInterval1D(double min, double max) {
    double firstValue = StdRandom.uniform(min, max);
    double secondValue = StdRandom.uniform(min, max);
    if (firstValue > secondValue) {
        double temp = firstValue;
        firstValue = secondValue;
        secondValue = temp;
    }
    Interval1D interval = new Interval1D(firstValue, secondValue);
    return interval;
}
Also used : Interval1D(edu.princeton.cs.algs4.Interval1D)

Example 3 with Interval1D

use of edu.princeton.cs.algs4.Interval1D in project algorithms-sedgewick-wayne by reneargento.

the class Exercise3 method drawAndCreateIntervals.

private static void drawAndCreateIntervals(Interval2D[] intervals, double min, double max) {
    StdDraw.setCanvasSize(1024, 512);
    StdDraw.setPenRadius(.015);
    StdDraw.setXscale(min, max);
    StdDraw.setYscale(min, max);
    for (int i = 0; i < intervals.length; i++) {
        Interval1D width = generateInterval1D(min, max);
        Interval1D height = generateInterval1D(min, max);
        Interval2D interval = new Interval2D(width, height);
        interval.draw();
        intervals[i] = interval;
        intervalMap.put(interval, new Interval1D[] { width, height });
    }
}
Also used : Interval1D(edu.princeton.cs.algs4.Interval1D) Interval2D(edu.princeton.cs.algs4.Interval2D)

Example 4 with Interval1D

use of edu.princeton.cs.algs4.Interval1D in project AlgorithmsSolutions by Allenskoo856.

the class Ex03 method main.

public static void main(String[] args) {
    int N = Integer.parseInt(args[0]);
    double min = Double.parseDouble(args[1]);
    double max = Double.parseDouble(args[2]);
    StdDraw.setXscale(min, max);
    StdDraw.setYscale(min, max);
    Point2D[] leftTopPoints = new Point2D[N];
    Point2D[] rightBottomPoints = new Point2D[N];
    Interval2D[] intervals = new Interval2D[N];
    for (int i = 0; i < N; i++) {
        double a = StdRandom.uniform(min, max);
        double b = StdRandom.uniform(min, max);
        double left, right, top, bottom;
        Interval1D x, y;
        if (a < b) {
            left = a;
            right = b;
        } else {
            left = b;
            right = a;
        }
        x = new Interval1D(left, right);
        a = StdRandom.uniform(min, max);
        b = StdRandom.uniform(min, max);
        if (a < b) {
            top = a;
            bottom = b;
        } else {
            top = b;
            bottom = a;
        }
        y = new Interval1D(top, bottom);
        leftTopPoints[i] = new Point2D(left, top);
        rightBottomPoints[i] = new Point2D(right, bottom);
        intervals[i] = new Interval2D(x, y);
        intervals[i].draw();
    }
    int containNum = 0, intervalNum = 0;
    for (int i = 0; i < N - 1; i++) {
        for (int j = 0; j < N; j++) {
            if (j > i && intervals[i].intersects(intervals[j])) {
                intervalNum++;
            }
            if (j != i && intervals[i].contains(leftTopPoints[j]) && intervals[i].contains(rightBottomPoints[j])) {
                containNum++;
            }
        }
    }
    System.out.println("Interval count: " + intervalNum);
    System.out.println("Contain count: " + containNum);
}
Also used : Interval1D(edu.princeton.cs.algs4.Interval1D) Point2D(edu.princeton.cs.algs4.Point2D) Interval2D(edu.princeton.cs.algs4.Interval2D)

Example 5 with Interval1D

use of edu.princeton.cs.algs4.Interval1D in project AlgorithmsSolutions by Allenskoo856.

the class Ex02 method main.

public static void main(String[] args) {
    int N = Integer.parseInt(args[0]);
    Interval1D[] intervals = new Interval1D[N];
    for (int i = 0; i < N; i++) {
        intervals[i] = new Interval1D(StdIn.readDouble(), StdIn.readDouble());
    }
    if (N > 2) {
        for (int i = 0; i < N - 1; i++) {
            for (int j = i + 1; j < N; j++) {
                if (intervals[i].intersects(intervals[j])) {
                    System.out.println(intervals[i] + " intersects " + intervals[j]);
                }
            }
        }
    }
}
Also used : Interval1D(edu.princeton.cs.algs4.Interval1D)

Aggregations

Interval1D (edu.princeton.cs.algs4.Interval1D)6 Interval2D (edu.princeton.cs.algs4.Interval2D)2 Point2D (edu.princeton.cs.algs4.Point2D)1