KiCad PCB EDA Suite
Loading...
Searching...
No Matches
box2_minmax.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 Alex Shvartzkop <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef __BOX2_MINMAX_H
22#define __BOX2_MINMAX_H
23
24#include <math/box2.h>
25#include <geometry/seg.h>
26#include <geometry/shape_arc.h>
27
28
33{
34 BOX2I_MINMAX() : m_Left( 0 ), m_Top( 0 ), m_Right( 0 ), m_Bottom( 0 ) {}
35
36 BOX2I_MINMAX( int aLeft, int aTop, int aRight, int aBottom ) :
37 m_Left( aLeft ), m_Top( aTop ), m_Right( aRight ), m_Bottom( aBottom )
38 {
39 }
40
41 BOX2I_MINMAX( const VECTOR2I& aPt ) : BOX2I_MINMAX( aPt.x, aPt.y ) {}
42
43 BOX2I_MINMAX( int aX, int aY ) : m_Left( aX ), m_Top( aY ), m_Right( aX ), m_Bottom( aY ) {}
44
45 BOX2I_MINMAX( const BOX2I& aBox ) :
46 m_Left( aBox.GetLeft() ), m_Top( aBox.GetTop() ), m_Right( aBox.GetRight() ),
47 m_Bottom( aBox.GetBottom() )
48 {
49 }
50
51 BOX2I_MINMAX( const VECTOR2I& aA, const VECTOR2I& aB )
52 {
53 m_Left = std::min( aA.x, aB.x );
54 m_Right = std::max( aA.x, aB.x );
55 m_Top = std::min( aA.y, aB.y );
56 m_Bottom = std::max( aA.y, aB.y );
57 }
58
59 operator BOX2I()
60 {
61 return BOX2ISafe( VECTOR2I( m_Left, m_Top ),
62 VECTOR2L( int64_t( m_Right ) - m_Left, int64_t( m_Bottom ) - m_Top ) );
63 }
64
65 BOX2I_MINMAX( const SHAPE_ARC& aArc ) : BOX2I_MINMAX( aArc.BBox() ) {}
66
67 BOX2I_MINMAX( const SEG& aSeg ) : BOX2I_MINMAX( aSeg.A, aSeg.B ) {}
68
69 inline bool Intersects( const BOX2I_MINMAX& aOther ) const
70 {
71 // calculate the left common area coordinate:
72 int left = std::max( m_Left, aOther.m_Left );
73 // calculate the right common area coordinate:
74 int right = std::min( m_Right, aOther.m_Right );
75 // calculate the upper common area coordinate:
76 int top = std::max( m_Top, aOther.m_Top );
77 // calculate the lower common area coordinate:
78 int bottom = std::min( m_Bottom, aOther.m_Bottom );
79
80 // if a common area exists, it must have a positive (null accepted) size
81 return left <= right && top <= bottom;
82 }
83
84 void Merge( const VECTOR2I& aPt )
85 {
86 m_Left = std::min( m_Left, aPt.x );
87 m_Right = std::max( m_Right, aPt.x );
88 m_Top = std::min( m_Top, aPt.y );
89 m_Bottom = std::max( m_Bottom, aPt.y );
90 }
91
93 {
94 int cx = ( (int64_t) m_Left + m_Right ) / 2;
95 int cy = ( (int64_t) m_Top + m_Bottom ) / 2;
96
97 return VECTOR2I( cx, cy );
98 }
99
100 double GetDiameter() const
101 {
102 VECTOR2L start( m_Left, m_Top );
104 VECTOR2L d = end - start;
105
106 return std::hypot( d.x, d.y );
107 }
108
110 int m_Top;
113};
114
115
116#endif
constexpr BOX2I BOX2ISafe(const BOX2D &aInput)
Definition box2.h:925
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
Definition seg.h:38
BOX2I_MINMAX(const VECTOR2I &aA, const VECTOR2I &aB)
Definition box2_minmax.h:51
BOX2I_MINMAX(const SEG &aSeg)
Definition box2_minmax.h:67
bool Intersects(const BOX2I_MINMAX &aOther) const
Definition box2_minmax.h:69
BOX2I_MINMAX(int aX, int aY)
Definition box2_minmax.h:43
double GetDiameter() const
BOX2I_MINMAX(const SHAPE_ARC &aArc)
Definition box2_minmax.h:65
void Merge(const VECTOR2I &aPt)
Definition box2_minmax.h:84
BOX2I_MINMAX(const VECTOR2I &aPt)
Definition box2_minmax.h:41
BOX2I_MINMAX(const BOX2I &aBox)
Definition box2_minmax.h:45
VECTOR2I GetCenter() const
Definition box2_minmax.h:92
BOX2I_MINMAX(int aLeft, int aTop, int aRight, int aBottom)
Definition box2_minmax.h:36
KIBIS top(path, &reporter)
VECTOR2I end
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< int64_t > VECTOR2L
Definition vector2d.h:684