001/*******************************************************************************
002 * This software is provided as a supplement to the authors' textbooks on digital
003 * image processing published by Springer-Verlag in various languages and editions.
004 * Permission to use and distribute this software is granted under the BSD 2-Clause
005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause).
006 * Copyright (c) 2006-2023 Wilhelm Burger, Mark J. Burge. All rights reserved.
007 * Visit https://imagingbook.com for additional details.
008 ******************************************************************************/
009package imagingbook.common.geometry.basic;
010
011import imagingbook.common.geometry.basic.Pnt2d.PntDouble;
012import imagingbook.common.geometry.basic.Pnt2d.PntInt;
013import imagingbook.common.geometry.shape.ShapeProducer;
014
015import java.awt.Shape;
016import java.awt.geom.Path2D;
017
018/**
019 * Described a 2D line segment defined by two end points. Instances are immutable.
020 *
021 * @author WB
022 */
023public class LineSegment2d implements ShapeProducer {
024        
025        private final Pnt2d p1, p2;
026        
027        /**
028         * Constructor for arbitrary 2D points.
029         * @param p1 first end point
030         * @param p2 second end point
031         */
032        public LineSegment2d(Pnt2d p1, Pnt2d p2) {
033                this.p1 = p1;
034                this.p2 = p2;
035        }
036        
037        /**
038         * Constructor for integer coordinates.
039         * @param x1 x-coordinate of first point
040         * @param y1 y-coordinate of first point
041         * @param x2 x-coordinate of second point
042         * @param y2 y-coordinate of second point
043         */
044        public LineSegment2d(int x1, int y1, int x2, int y2) {
045                this.p1 = PntInt.from(x1, y1);
046                this.p2 = PntInt.from(x2, y2);
047        }
048        
049        /**
050         * Constructor for double coordinates.
051         * @param x1 x-coordinate of first point
052         * @param y1 y-coordinate of first point
053         * @param x2 x-coordinate of second point
054         * @param y2 y-coordinate of second point
055         */
056        public LineSegment2d(double x1, double y1, double x2, double y2) {
057                this.p1 = PntDouble.from(x1, y1);
058                this.p2 = PntDouble.from(x2, y2);
059        }
060        
061        /**
062         * Returns the first end point.
063         * @return the first end point
064         */
065        public Pnt2d getP1() {
066                return p1;
067        }
068        
069        /**
070         * Returns the second end point.
071         * @return the second end point
072         */
073        public Pnt2d getP2() {
074                return p2;
075        }
076        
077        // ------------------------------------------------------------
078
079        @Override
080        public Shape getShape(double scale) {
081                Path2D path = new Path2D.Double();
082                path.moveTo(p1.getX(), p1.getY());
083                path.lineTo(p2.getX(), p2.getY());
084                return path;
085        }
086        
087}