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