1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.utils.geom;
\r
14 import java.awt.Rectangle;
\r
15 import java.awt.Shape;
\r
16 import java.awt.geom.AffineTransform;
\r
17 import java.awt.geom.PathIterator;
\r
18 import java.awt.geom.Point2D;
\r
19 import java.awt.geom.Rectangle2D;
\r
20 import java.util.Arrays;
\r
23 * Describes a line segment, either linear, quadratic or cubic
\r
25 * @author Toni Kalajainen
\r
27 public class LineSegment implements Shape {
\r
28 private double line[];
\r
29 public LineSegment(double x0, double y0, double x1, double y1)
\r
31 line = new double[] {x0, y0, x1, y1};
\r
33 public LineSegment(double x0, double y0, double x1, double y1, double x2, double y2)
\r
35 line = new double[] {x0, y0, x1, y1, x2, y2};
\r
37 public LineSegment(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3)
\r
39 line = new double[] {x0, y0, x1, y1, x2, y2, x3, y3};
\r
41 public LineSegment(double data[])
\r
43 assert(data.length==4 || data.length==6 || data.length==8);
\r
44 line = Arrays.copyOf(data, data.length);
\r
46 public double[] getLine() {
\r
49 public int getDegree() {
\r
50 return (line.length-2)/2;
\r
52 public Point2D getStartPos(Point2D pt)
\r
54 if (pt==null) return new Point2D.Double(line[0], line[1]);
\r
55 pt.setLocation(line[0], line[1]);
\r
58 public Point2D getEndPos(Point2D pt)
\r
60 int p = line.length-2;
\r
61 if (pt==null) return new Point2D.Double(line[p], line[p+1]);
\r
62 pt.setLocation(line[p], line[p+1]);
\r
65 public void setData(double data[])
\r
67 assert(data.length==4 || data.length==6 || data.length==8);
\r
68 line = Arrays.copyOf(data, data.length);
\r
74 public boolean contains(Point2D p) {
\r
78 public boolean contains(Rectangle2D r) {
\r
82 public boolean contains(double x, double y) {
\r
86 public boolean contains(double x, double y, double w, double h) {
\r
90 public Rectangle getBounds() {
\r
94 public Rectangle2D getBounds2D() {
\r
98 public PathIterator getPathIterator(AffineTransform at) {
\r
102 public PathIterator getPathIterator(AffineTransform at, double flatness) {
\r
106 public boolean intersects(Rectangle2D r) {
\r
110 public boolean intersects(double x, double y, double w, double h) {
\r