1 /*******************************************************************
2 
3 Part of the Fritzing project - http://fritzing.org
4 Copyright (c) 2007-2014 Fachhochschule Potsdam - http://fh-potsdam.de
5 
6 Fritzing is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 Fritzing 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 Fritzing.  If not, see <http://www.gnu.org/licenses/>.
18 
19 ********************************************************************
20 
21 $Revision: 2979 $:
22 $Author: irascibl@gmail.com $:
23 $Date: 2013-02-26 16:26:03 +0100 (Di, 26. Feb 2013) $
24 
25 ********************************************************************/
26 
27 #ifndef TILEUTILS
28 #define TILEUTILS
29 
30 #include "tile.h"
31 
32 static const int TILEFACTOR = 1000;
33 
fasterRealToTile(double x)34 inline int fasterRealToTile(double x) {
35         return qRound(x * TILEFACTOR);
36 }
37 
realsToTile(TileRect & tileRect,double l,double t,double r,double b)38 inline void realsToTile(TileRect & tileRect, double l, double t, double r, double b) {
39         tileRect.xmini = fasterRealToTile(l);
40         tileRect.ymini = fasterRealToTile(t);
41         tileRect.xmaxi = fasterRealToTile(r);
42         tileRect.ymaxi = fasterRealToTile(b);
43 }
44 
qrectToTile(QRectF & rect,TileRect & tileRect)45 inline void qrectToTile(QRectF & rect, TileRect & tileRect) {
46 	realsToTile(tileRect, rect.left(), rect.top(), rect.right(), rect.bottom());
47 }
48 
realToTile(double x)49 inline int realToTile(double x) {
50 	return qRound(x * TILEFACTOR);
51 }
52 
tileToReal(int x)53 inline double tileToReal(int x) {
54 	return x / ((double) TILEFACTOR);
55 }
56 
tileRectToQRect(TileRect & tileRect,QRectF & rect)57 inline void tileRectToQRect(TileRect & tileRect, QRectF & rect) {
58 	rect.setCoords(tileToReal(tileRect.xmini), tileToReal(tileRect.ymini), tileToReal(tileRect.xmaxi), tileToReal(tileRect.ymaxi));
59 }
60 
tileToQRect(Tile * tile,QRectF & rect)61 inline void tileToQRect(Tile * tile, QRectF & rect) {
62 	TileRect tileRect;
63 	TiToRect(tile, &tileRect);
64 	tileRectToQRect(tileRect, rect);
65 }
66 
tileRotate90(TileRect & tileRect,TileRect & tileRect90)67 inline void tileRotate90(TileRect & tileRect, TileRect & tileRect90)
68 {
69 	// x' = x*cos - y*sin
70 	// y' = x*sin + y*cos
71 	// where cos90 = 0 and sin90 = 1 (effectively clockwise)
72 
73 	// rotate top right corner of rect
74 	tileRect90.xmini = -tileRect.ymaxi;
75 	tileRect90.ymini = tileRect.xmini;
76 
77 	// swap width and height
78 	tileRect90.xmaxi = tileRect90.xmini + (tileRect.ymaxi - tileRect.ymini);
79 	tileRect90.ymaxi = tileRect90.ymini + (tileRect.xmaxi - tileRect.xmini);
80 }
81 
82 
tileUnrotate90(TileRect & tileRect90,TileRect & tileRect)83 inline void tileUnrotate90(TileRect & tileRect90, TileRect & tileRect)
84 {
85 	tileRect.xmini = tileRect90.ymini;
86 	tileRect.ymaxi = -tileRect90.xmini;
87 
88 	// swap width and height
89 	tileRect.xmaxi = tileRect.xmini + (tileRect90.ymaxi - tileRect90.ymini);
90 	tileRect.ymini = tileRect.ymaxi - (tileRect90.xmaxi - tileRect90.xmini);
91 }
92 
93 #endif
94