KiCad PCB EDA Suite
Loading...
Searching...
No Matches
item_drawing_utils.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) 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
25
26#include <array>
27
29#include <geometry/seg.h>
30
31using namespace KIGFX;
32
33void KIGFX::DrawCross( GAL& aGal, const VECTOR2I& aPosition, int aSize )
34{
35 const int size = aSize / 2;
36 aGal.DrawLine( aPosition - VECTOR2I( size, 0 ), aPosition + VECTOR2I( size, 0 ) );
37 aGal.DrawLine( aPosition - VECTOR2I( 0, size ), aPosition + VECTOR2I( 0, size ) );
38}
39
40
41void KIGFX::DrawDashedLine( GAL& aGal, const SEG& aSeg, double aDashSize )
42{
43 const std::array<double, 2> strokes = { aDashSize, aDashSize / 2 };
44 const double dashCycleLen = strokes[0] + strokes[1];
45 // The dash cycle length must be at least 1 pixel.
46 wxASSERT( dashCycleLen * aGal.GetWorldScale() > 1 );
47
48 const BOX2I clip = BOX2I::ByCorners( aSeg.A, aSeg.B );
49
50 const double theta = atan2( aSeg.B.y - aSeg.A.y, aSeg.B.x - aSeg.A.x );
51
52 const VECTOR2D cycleVec{
53 dashCycleLen * cos( theta ),
54 dashCycleLen * sin( theta ),
55 };
56 const VECTOR2D dashVec{
57 strokes[0] * cos( theta ),
58 strokes[0] * sin( theta ),
59 };
60
61 unsigned cyclei = 0;
62 while( true )
63 {
64 const VECTOR2D dashStart = aSeg.A + cycleVec * cyclei;
65 const VECTOR2D dashEnd = dashStart + dashVec;
66
67 // Drawing each segment can be done rounded to ints.
68 SEG dashSeg{ KiROUND( dashStart ), KiROUND( dashEnd ) };
69
70 if( ClipLine( &clip, dashSeg.A.x, dashSeg.A.y, dashSeg.B.x, dashSeg.B.y ) )
71 break;
72
73 aGal.DrawLine( dashSeg.A, dashSeg.B );
74 ++cyclei;
75 }
76}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:969
static constexpr BOX2< VECTOR2I > ByCorners(const VECTOR2I &aCorner1, const VECTOR2I &aCorner2)
Definition: box2.h:70
Abstract interface for drawing on a 2D-surface.
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
double GetWorldScale() const
Get the world scale.
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
VECTOR2I B
Definition: seg.h:50
a few functions useful in geometry calculations.
bool ClipLine(const BOX2I *aClipBox, int &x1, int &y1, int &x2, int &y2)
Test if any part of a line falls within the bounds of a rectangle.
Utility functions for drawing compound items (i.e.
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
void DrawCross(GAL &aGal, const VECTOR2I &aPosition, int aSize)
Draw a cross at a given position.
void DrawDashedLine(GAL &aGal, const SEG &aSeg, double aDashSize)
Draw a dashed line.
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691