generate Parallel Lines - Java java.lang

Java examples for java.lang:Math Geometry Line

Description

generate Parallel Lines

Demo Code


//package com.java2s;

import java.awt.geom.Line2D;

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<Line2D> generateParallelLines(double gap,
            double angle, int width, int height) {
        List<Line2D> lines = new ArrayList<Line2D>();
        double xOffset = (1 / Math.tan(angle)) * height;
        double gapInX = Math.abs(gap / Math.sin(angle));
        int nlines = (int) ((width + Math.abs(xOffset)) / gapInX) + 1;
        double xTop;
        double xButtom;
        if (xOffset >= 0) {
            xTop = 0 - xOffset;/*from w w w.  j  a  v a 2s.c  o m*/
            xButtom = 0;
        } else {
            xTop = 0;
            xButtom = 0 + xOffset;
        }
        Line2D l2d;
        for (int i = 0; i < nlines; i++) {

            if (i == 0) {
                l2d = new Line2D.Double(xTop, 0, xButtom, height);
            } else {
                xTop += gapInX;
                xButtom += gapInX;
                l2d = new Line2D.Double(xTop, 0, xButtom, height);

            }
            lines.add(l2d);
        }

        return lines;
    }
}

Related Tutorials