KiCad PCB EDA Suite
Loading...
Searching...
No Matches
half_line.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2024 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#pragma once
25
26#include <optional>
27
28#include <geometry/seg.h>
29#include <math/box2.h>
30
31/*
32 * A geometric half-line of infinite length, starting at a given point and extending infinitely.
33 * A.k.a. a ray.
34 *
35 * In terms of geometric ops, a SEG would probably do in most cases, as it
36 * has the same definition, but a separate class is more explicit and also
37 * allows compile-time reasoning about the meaning of the object through
38 * the type system.
39 */
41{
42public:
47 HALF_LINE( const SEG& aSeg ) : m_seg( aSeg ) {}
48
49 HALF_LINE( const VECTOR2I& aStart, const VECTOR2I& aOtherContainedPoint ) :
50 m_seg( aStart, aOtherContainedPoint )
51 {
52 }
53
57 const VECTOR2I& GetStart() const { return m_seg.A; }
58
62 const VECTOR2I& GetContainedPoint() const { return m_seg.B; }
63
64 bool Contains( const VECTOR2I& aPoint ) const;
65
66 OPT_VECTOR2I Intersect( const SEG& aSeg ) const;
67
68 OPT_VECTOR2I Intersect( const HALF_LINE& aOther ) const;
69
75 VECTOR2I NearestPoint( const VECTOR2I& aPoint ) const;
76
80 bool operator==( const HALF_LINE& aOther ) const { return m_seg == aOther.m_seg; }
81
87 const SEG& GetContainedSeg() const { return m_seg; }
88
89private:
92};
93
94
95std::optional<SEG> ClipHalfLineToBox( const HALF_LINE& aRay, const BOX2I& aBox );
OPT_VECTOR2I Intersect(const SEG &aSeg) const
Definition: half_line.cpp:57
const SEG & GetContainedSeg() const
Gets the (one of the infinite number of) segments that the ray passes through.
Definition: half_line.h:87
const VECTOR2I & GetStart() const
Get the start point of the ray.
Definition: half_line.h:57
HALF_LINE(const SEG &aSeg)
Construct a ray from a segment - the ray will start at the segment's A point and extend infinitely in...
Definition: half_line.h:47
const VECTOR2I & GetContainedPoint() const
Get one (of the infinite number) of points that the ray passes through.
Definition: half_line.h:62
SEG m_seg
Internally, we can represent a just a segment that the ray passes through.
Definition: half_line.h:91
HALF_LINE(const VECTOR2I &aStart, const VECTOR2I &aOtherContainedPoint)
Definition: half_line.h:49
bool Contains(const VECTOR2I &aPoint) const
Definition: half_line.cpp:41
VECTOR2I NearestPoint(const VECTOR2I &aPoint) const
Get the nearest point on the ray to the given point.
Definition: half_line.cpp:110
bool operator==(const HALF_LINE &aOther) const
Based on the ray being identically defined.
Definition: half_line.h:80
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
VECTOR2I B
Definition: seg.h:50
std::optional< SEG > ClipHalfLineToBox(const HALF_LINE &aRay, const BOX2I &aBox)
std::optional< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:39