KiCad PCB EDA Suite
centreline_rect_item.cpp
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) 2017 Kicad Developers, see change_log.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 
27 #include <view/view.h>
28 
29 using namespace KIGFX::PREVIEW;
30 
32  const VECTOR2D& aClEnd, double aAspect )
33 {
34  SHAPE_POLY_SET poly;
35  poly.NewOutline();
36 
37  /*
38  * The point layout of the rectangle goes like this,
39  * but start/end don't have to be horz/vert
40  *
41  * 0 ---------------- 1 -----
42  * | | ^
43  * s--------cl------->e |cl|/aspect
44  * | | v
45  * 3----------------- 2 -----
46  */
47 
48  // vector down the centre line of the rectangle
49  VECTOR2D cl = aClEnd - aClStart;
50 
51  // don't allow degenerate polygons
52  if( cl.x == 0 && cl.y == 0 )
53  cl.x = 1.0;
54 
55  // the "side" of the rectangle is the centre line rotated by 90 deg
56  // and scaled by the aspect ratio
57  VECTOR2D side = cl.Rotate( M_PI / 2.0 ) * aAspect;
58 
59  VECTOR2D pt = aClStart + ( side / 2.0 );
60  poly.Append( pt );
61 
62  pt += cl;
63  poly.Append( pt );
64 
65  pt -= side;
66  poly.Append( pt );
67 
68  pt -= cl;
69  poly.Append( pt );
70 
71  return poly;
72 }
73 
74 
76  double aAspect ) :
77  m_geomMgr( aGeomMgr ),
78  m_aspect( aAspect )
79 {
80 }
81 
82 
84 {
86 }
87 
88 
90 {
91  return getOutline().BBox();
92 }
93 
94 
96 {
97  KIGFX::GAL& gal = *aView->GetGAL();
98 
100  gal.DrawPolygon( getOutline() );
101 }
Represent a very simple geometry manager for items that have a start and end point.
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:189
void drawPreviewShape(KIGFX::VIEW *aView) const override
Draw the preview onto the given GAL.
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
Represent a set of closed polygons.
CENTRELINE_RECT_ITEM(const TWO_POINT_GEOMETRY_MANAGER &aGeomMgr, double aAspect)
Gets the bounding box of the rectangle.
int NewOutline()
Creates a new hole in a given outline.
VECTOR2< T > Rotate(double aAngle) const
Rotate the vector by a given angle.
Definition: vector2d.h:371
virtual const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
virtual void DrawPolygon(const std::deque< VECTOR2D > &aPointList)
Draw a polygon.
SHAPE_POLY_SET getOutline() const
< Get the rectangular outline
static SHAPE_POLY_SET getRectangleAlongCentreLine(const VECTOR2D &aClStart, const VECTOR2D &aClEnd, double aAspect)
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Abstract interface for drawing on a 2D-surface.
const TWO_POINT_GEOMETRY_MANAGER & m_geomMgr
The aspect ratio of the rectangle to draw.
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Add a new vertex to the contour indexed by aOutline and aHole (defaults to the outline of the last po...