File: viewport.cpp

package info (click to toggle)
golly 3.3-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 20,176 kB
  • sloc: cpp: 72,638; ansic: 25,919; python: 7,921; sh: 4,245; objc: 3,721; java: 2,781; xml: 1,362; makefile: 530; javascript: 279; perl: 69
file content (239 lines) | stat: -rw-r--r-- 5,583 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// This file is part of Golly.
// See docs/License.html for the copyright notice.

#include "viewport.h"
#include "lifealgo.h"
#include <cmath>

int MAX_MAG = 4 ;   // default maximum cell size is 2^4

using namespace std ;

void viewport::init() {
   x = 0 ;
   y = 0 ;
   height = width = 8 ;
   mag = 0 ;
   x0 = 0 ;
   y0 = 0 ;
   x0f = 0 ;
   y0f = 0 ;
   xymf = 0 ;
}
void viewport::zoom() {
   if (mag >= MAX_MAG)
      return ;
   mag++ ;
   reposition() ;
}
void viewport::zoom(int xx, int yy) {
   if (mag >= MAX_MAG)
      return ;
   pair<bigint, bigint> oldpos = at(xx, yy);    // save cell pos for use below
   int x2c = xx * 2 - getxmax() ;
   bigint o = x2c ;
   o.mulpow2(-mag-2) ;
   x += o ;
   int y2c = yy * 2 - getymax() ;
   o = y2c ;
   o.mulpow2(-mag-2) ;
   y += o ;
   mag++ ;
   reposition() ;
   // adjust cell position if necessary to avoid any drift
   if (mag >= 0) {
      pair<bigint, bigint> newpos = at(xx, yy);
      bigint xdrift = newpos.first;
      bigint ydrift = newpos.second;
      xdrift -= oldpos.first;
      ydrift -= oldpos.second;
      // drifts will be -1, 0 or 1
      if (xdrift != 0) move(-xdrift.toint() << mag, 0);
      if (ydrift != 0) move(0, -ydrift.toint() << mag);
   }
}
void viewport::unzoom() {
   mag-- ;
   reposition() ;
}
void viewport::unzoom(int xx, int yy) {
   pair<bigint, bigint> oldpos = at(xx, yy);    // save cell pos for use below
   mag-- ;
   int x2c = xx * 2 - getxmax() ;
   bigint o = x2c ;
   o.mulpow2(-mag-2) ;
   x -= o ;
   int y2c = yy * 2 - getymax() ;
   o = y2c ;
   o.mulpow2(-mag-2) ;
   y -= o ;
   reposition() ;
   if (mag >= 0) {
      // adjust cell position if necessary to avoid any drift
      pair<bigint, bigint> newpos = at(xx, yy);
      bigint xdrift = newpos.first;
      bigint ydrift = newpos.second;
      xdrift -= oldpos.first;
      ydrift -= oldpos.second;
      // drifts will be -1, 0 or 1
      if (xdrift != 0) move(-xdrift.toint() << mag, 0);
      if (ydrift != 0) move(0, -ydrift.toint() << mag);
   }
}
pair<bigint, bigint> viewport::at(int x, int y) {
   bigint rx = x ;
   bigint ry = y ;
   rx.mulpow2(-mag) ;
   ry.mulpow2(-mag) ;
   rx += x0 ;
   ry += y0 ;
   return pair<bigint, bigint>(rx, ry) ;
}
pair<double, double> viewport::atf(int x, int y) {
   return pair<double, double>(x0f + x * xymf, y0f + y * xymf) ;
}
/**
 *   Returns the screen position of a particular pixel.  Note that this
 *   is a tiny bit more complicated than you might expect, because it
 *   has to take into account exactly how a life algorithm compresses
 *   multiple pixels into a single pixel (which depends not only on the
 *   lifealgo, but in the case of qlifealgo, *also* depends on the
 *   generation count).  In the case of mag < 0, it always returns
 *   the upper left pixel; it's up to the caller to adjust when
 *   mag<0.
 */
pair<int,int> viewport::screenPosOf(bigint x, bigint y, lifealgo *algo) {
   if (mag < 0) {
      bigint xx0 = x0 ;
      bigint yy0 = y0 ;
      algo->lowerRightPixel(xx0, yy0, mag) ;
      y -= yy0 ;
      x -= xx0 ;
   } else {
      x -= x0 ;
      y -= y0 ;
   }
   x.mulpow2(mag) ;
   y.mulpow2(mag) ;

   int xx = 0 ;
   int yy = 0 ;
   
/* AKT: don't do this clipping as it makes it harder to
        calculate an accurate paste rectangle
   if (x < 0)
     xx = -1 ;
   else if (x > getxmax())
     xx = getxmax() + 1 ;
   else
     xx = x.toint() ;
     
   if (y < 0)
     yy = -1 ;
   else if (y > getymax())
     yy = getymax() + 1 ;
   else
     yy = y.toint() ;
*/
   
   if (x > bigint::maxint)
      xx = INT_MAX ;
   else if (x < bigint::minint)
      xx = INT_MIN ;
   else
      xx = x.toint() ;
   
   if (y > bigint::maxint)
      yy = INT_MAX ;
   else if (y < bigint::minint)
      yy = INT_MIN ;
   else
      yy = y.toint() ;
     
   return pair<int,int>(xx,yy) ;
}
void viewport::move(int dx, int dy) {
   // dx and dy are in pixels
   if (mag > 0) {
      dx /= (1 << mag) ;
      dy /= (1 << mag) ;
   }
   bigint addx = dx ;
   bigint addy = dy ;
   if (mag < 0) {
      addx <<= -mag ;
      addy <<= -mag ;
   }
   x += addx ;
   y += addy ;
   reposition() ;
}
void viewport::resize(int newwidth, int newheight) {
   width = newwidth ;
   height = newheight ;
   reposition() ;
}
void viewport::center() {
   x = 0 ;
   y = 0 ;
   reposition() ;
}
void viewport::reposition() {
   xymf = pow(2.0, -mag) ;
   bigint w = 1 + getxmax() ;
   w.mulpow2(-mag) ;
   w >>= 1 ;
   x0 = x ;
   x0 -= w ;
   w = 1 + getymax() ;
   w.mulpow2(-mag) ;
   w >>= 1 ;
   y0 = y ;
   y0 -= w ;
   y0f = y0.todouble() ;
   x0f = x0.todouble() ;
}
void viewport::setpositionmag(const bigint &xarg, const bigint &yarg,
                              int magarg) {
   x = xarg ;
   y = yarg ;
   mag = magarg ;
   reposition() ;
}
/*
 *   This is only called by fit.  We find an x/y location that
 *   centers things optimally.
 */
void viewport::setpositionmag(const bigint &xmin, const bigint &xmax,
                              const bigint &ymin, const bigint &ymax,
                              int magarg) {
   mag = magarg ;
   x = xmax ;
   x += xmin ;
   x += 1 ;
   x >>= 1 ;
   y = ymax ;
   y += ymin ;
   y += 1 ;
   y >>= 1 ;
   reposition() ;
}
int viewport::contains(const bigint &xarg, const bigint &yarg) {
   if (xarg < x0 || yarg < y0)
      return 0 ;
   bigint t = getxmax() ;
   t += 1 ;
   t.mulpow2(-mag) ;
   t -= 1 ;
   t += x0 ;
   if (xarg > t)
      return 0 ;
   t = getymax() ;
   t += 1 ;
   t.mulpow2(-mag) ;
   t -= 1 ;
   t += y0 ;
   if (yarg > t)
      return 0 ;
   return 1 ;
}