InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PMLineSeg.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Ric Kamicar
6 //
7 // $Author$
8 //
9 // $DateTime$
10 //
11 // $Revision$
12 //
13 // $Change$
14 //
15 // Copyright 1997-2010 Adobe Systems Incorporated. All rights reserved.
16 //
17 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance
18 // with the terms of the Adobe license agreement accompanying it. If you have received
19 // this file from a source other than Adobe, then your use, modification, or
20 // distribution of it requires the prior written permission of Adobe.
21 //
22 //
23 // Simple class to hold two points. The line is vertically directional, meaning that top <= bottom.
24 // The line is primarily vertical, so the object is optimized for that.
25 // Also, UNLIKE PMReal, this class asserts if the object is ill-formed (PMRect silently normalizes).
26 //
27 //========================================================================================
28 
29 #ifndef __PMLineSeg__
30 #define __PMLineSeg__
31 
32 #include "PMPoint.h"
33 
34 
37 class PMLineSeg
38 {
39 public:
43  { }
44 
52  PMLineSeg(const PMPoint& P1, const PMPoint& P2) :
53  fTop(P1), fBottom(P2)
54  {
55  if (fTop.Y() > fBottom.Y()) {
56  fTop = P2;
57  fBottom = P1;
58  }
59  ASSERT(fTop.Y() <= fBottom.Y());
60  }
61 
68  PMLineSeg(const PMPoint& P1, const PMReal& xDistance, const PMReal& yDistance) :
69  fTop(P1)
70  {
71  ASSERT(yDistance >= 0);
72  fBottom = PMPoint(fTop.X() + xDistance, fTop.Y() + yDistance);
73  }
74 
82  PMLineSeg(const PMReal& p1x, const PMReal& p1y, const PMReal& p2x, const PMReal& p2y)
83  {
84  ASSERT(p1y <= p2y);
85  fTop = PMPoint(p1x, p1y);
86  fBottom = PMPoint(p2x, p2y);
87  }
88 
91  bool16 IsHorizontal() const
92  {
93  return fBottom.Y() == fTop.Y();
94  }
95 
98  bool16 IsVertical() const
99  {
100  return fBottom.X() == fTop.X();
101  }
102 
105  PMReal DeltaX() const
106  {
107  return fBottom.X() - fTop.X();
108  }
109 
112  PMReal DeltaY() const
113  {
114  return fBottom.Y() - fTop.Y();
115  }
116 
117  // Accessor Methods
120  const PMPoint& TopPoint() const
121  {
122  return fTop;
123  }
124 
127  const PMPoint& BottomPoint() const
128  {
129  return fBottom;
130  }
131 
132 #if ZERO
133  PMPoint& TopPoint()
134  {
135  return fTop;
136  }
137  void TopPoint(const PMPoint& newP1)
138  {
139  fTop = newP1;
140  }
141 
143  {
144  return fBottom;
145  }
146  void BottomPoint(const PMPoint& newP2)
147  {
148  fBottom = newP2;
149  }
150 #endif
151 
154  const PMReal& Top() const
155  {
156  return fTop.Y();
157  }
158 
164  {
165  return fTop.Y();
166  }
167 
171  void Top(const PMReal& newP1y)
172  {
173  fTop.Y(newP1y);
174  ASSERT(fTop.Y() <= fBottom.Y());
175  }
176 
179  const PMReal& Bottom() const
180  {
181  return fBottom.Y();
182  }
188  {
189  return fBottom.Y();
190  }
194  void Bottom(const PMReal& newP2y)
195  {
196  fBottom.Y(newP2y);
197  ASSERT(fTop.Y() <= fBottom.Y());
198  }
199 
202  const PMReal& Left() const
203  {
204  return fTop.X() > fBottom.X() ? fBottom.X() : fTop.X();
205  }
209  {
210  return fTop.X() > fBottom.X() ? fBottom.X() : fTop.X();
211  }
215  void Left(const PMReal& newLeft)
216  {
217  if (fTop.X() > fBottom.X())
218  {
219  // The left point was also the bottom point. Will it still be?
220  ASSERT_MSG(fTop.X() >= newLeft, "Swapping left and right points. Is this really what you want?");
221  fBottom.X(newLeft);
222  }
223  else
224  {
225  ASSERT_MSG(newLeft <= fBottom.X(), "Swapping left and right points. Is this really what you want?");
226  fTop.X(newLeft);
227  }
228  }
229 
232  const PMReal& Right() const
233  {
234  return fTop.X() > fBottom.X() ? fTop.X() : fBottom.X();
235  }
239  {
240  return fTop.X() > fBottom.X() ? fTop.X() : fBottom.X();
241  }
245  void Right(const PMReal& newRight)
246  {
247  if (fTop.X() > fBottom.X())
248  {
249  // The right point was also the top point. Will it still be?
250  ASSERT_MSG(newRight >= fBottom.X(), "Swapping left and right points. Is this really what you want?");
251  fTop.X(newRight);
252  }
253  else
254  {
255  ASSERT_MSG(fTop.X() <= newRight, "Swapping left and right points. Is this really what you want?");
256  fBottom.X(newRight);
257  }
258  }
259 
264  void SetX(const PMReal& x1, const PMReal& x2)
265  {
266  fTop.X(x1);
267  fBottom.X(x2);
268  }
269 
274  void SetY(const PMReal& y1, const PMReal& y2)
275  {
276  ASSERT(y1 <= y2);
277 
278  fTop.Y(y1);
279  fBottom.Y(y2);
280  }
281 
286  void SetHorizontal(const PMReal& x1, const PMReal& x2)
287  {
288  fTop.X(x1);
289  fBottom.X(x2);
290  fTop.Y(0.);
291  fBottom.Y(0.);
292  }
293 
298  void SetVertical(const PMReal& y1, const PMReal& y2)
299  {
300  ASSERT(y1 <= y2);
301 
302  fTop.X(0.);
303  fBottom.X(0.);
304  fTop.Y(y1);
305  fBottom.Y(y2);
306  }
307 
313  PMLineSeg& MoveRel(const PMReal& dx, const PMReal& dy)
314  {
315  if (dx != 0.)
316  {
317  fTop.X() += dx;
318  fBottom.X() += dx;
319  }
320  if (dy != 0.)
321  {
322  fTop.Y() += dy;
323  fBottom.Y() += dy;
324  }
325  return *this;
326  }
327 
332  PMLineSeg& MoveRel(const PMPoint& point)
333  {
334  MoveRel(point.X(), point.Y());
335  return *this;
336  }
337 
338  // Comparison operator
344  friend bool16 operator ==(const PMLineSeg& a, const PMLineSeg& b);
350  friend bool16 operator !=(const PMLineSeg& a, const PMLineSeg& b);
351 
352 private:
353  PMPoint fTop;
354  PMPoint fBottom;
355 };
356 
357 
358 //----------------------------------------------------------------------------------------
359 // Inlines:
360 //----------------------------------------------------------------------------------------
361 
362 inline bool16 operator ==(const PMLineSeg& a, const PMLineSeg& b)
363 {
364  return a.TopPoint() == b.TopPoint() && a.BottomPoint() == b.BottomPoint();
365 }
366 
367 inline bool16 operator !=(const PMLineSeg& a, const PMLineSeg& b)
368 {
369  return a.TopPoint() != b.TopPoint() || a.BottomPoint() != b.BottomPoint();
370 }
371 
372 #endif
373