Code example for Set

Methods: add, clear

0
       intersectLine(line);
    } 
     
    public void intersectLine(Line2D line) {
        this.line = line;
        points.clear();
        float rx0 = (float) rectangle.getMinX();
        float ry0 = (float) rectangle.getMinY();
        float rx1 = (float) rectangle.getMaxX();
        float ry1 = (float) rectangle.getMaxY();
         
        float x1 = (float) line.getX1();
        float y1 = (float) line.getY1();
        float x2 = (float) line.getX2();
        float y2 = (float) line.getY2();
         
        float dy = y2 - y1;
        float dx = x2 - x1;
         
        if(dx != 0) {
            float m = dy/dx;
            float b = y1 - m*x1;
             
            // base of rect where y == ry0 
            float x = (ry0 - b) / m;
             
            if(rx0 <= x && x <= rx1) {
                points.add(new Point2D.Float(x, ry0));
            } 
             
            // top where y == ry1 
            x = (ry1 - b) / m;
            if(rx0 <= x && x <= rx1) {
                points.add(new Point2D.Float(x, ry1));
            } 
             
            // left side, where x == rx0 
            float y = m * rx0 + b;
            if(ry0 <= y && y <= ry1) {
                points.add(new Point2D.Float(rx0, y));
            } 
             
             
            // right side, where x == rx1 
            y = m * rx1 + b;
            if(ry0 <= y && y <= ry1) {
                points.add(new Point2D.Float(rx1, y));
            } 
             
        } else { 
             
            // base, where y == ry0 
            float x = x1;
            if(rx0 <= x && x <= rx1) {
                points.add(new Point2D.Float(x, ry0));
            } 
             
            // top, where y == ry1 
            x = x2;
            if(rx0 <= x && x <= rx1) {
                points.add(new Point2D.Float(x, ry1));
            } 
        } 
    } 
    public Line2D getLine() { 
        return line;