InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PMPoint.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Lonnie Millett
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 
24 #ifndef __PMPOINT__
25 #define __PMPOINT__
26 
27 #include "PMReal.h"
28 #include "SystemUtils.h"
29 #include "K2Vector.h"
30 
31 //----------------------------------------------------------------------------------------
32 // Forward declarations
33 //----------------------------------------------------------------------------------------
34 class PMPoint;
35 class PMRect;
36 class IPMStream;
37 
38 //----------------------------------------------------------------------------------------
39 // Typedefs
40 // The following types are designed primarily for use in parameter lists to aid in
41 // determining what coordinate space a method or function expects a parameter to be in.
42 // They are just synonyms for PMPoint
43 //----------------------------------------------------------------------------------------
44 typedef PMPoint PBPMPoint; // point in "Pasteboard" PMRealinates
45 typedef PMPoint IPMPoint; // point in "Inner" PMRealinates of page item
48 
49 //----------------------------------------------------------------------------------------
50 // PMPoint
51 //----------------------------------------------------------------------------------------
52 
53 // Math Operators
54 PMPoint operator +(const PMPoint& a, const PMPoint& b);
55 PMPoint operator -(const PMPoint& a, const PMPoint& b);
56 PMPoint operator -(const PMPoint& a);
57 PMPoint operator *(const PMReal& c, const PMPoint& pt);
58 PMPoint operator *(const PMPoint& pt, const PMReal& c);
59 PMPoint operator *(const PMPoint& a, const PMPoint& b);
60 PMPoint operator /(const PMPoint& numer, const PMPoint& denom);
61 PMPoint operator /(const PMPoint& numer, const PMReal& denom);
62 
63 
72 class PMPoint
73 {
74  public:
75  typedef base_type data_type;
76 
80  constexpr PMPoint() noexcept: x(0.0), y(0.0)
81  {}
82 
89  constexpr PMPoint(const PMPoint &) noexcept = default;
90  constexpr PMPoint(PMPoint &&) noexcept = default;
91  PMPoint &operator =(const PMPoint &) noexcept = default;
92  PMPoint &operator =(PMPoint &&) noexcept = default;
93 
100  constexpr PMPoint(const PMReal& x_orig, const PMReal& y_orig) noexcept : x(x_orig), y(y_orig)
101  {}
102 
108  constexpr PMPoint(const SysPoint& p) noexcept: x(double(SysPointH(p))), y(double(SysPointV(p)))
109  {}
110 
111  // Outbound Conversions
112 
119  friend SysPoint ToSys(const PMPoint& pt);
120 
121  // Accessor Methods
122 
128  const PMReal& X() const noexcept
129  { return x; }
130 
136  PMReal& X() noexcept
137  { return x; }
138 
144  void X(const PMReal& xParam) noexcept
145  { x = xParam; }
146 
152  const PMReal& Y() const noexcept
153  { return y; }
154 
160  PMReal& Y() noexcept
161  { return y; }
162 
168  void Y(const PMReal& yParam) noexcept
169  { y = yParam; }
170 
177  PMReal Distance (const PMPoint& otherPt) const
178  {
179  return (otherPt - *this).VectorLength();
180  }
181 
188  {
189  if( ::ToDouble(x) == 0.) //convert to double to get exact comparison
190  return abs(y); //avoid roundof error from sqrt(y*y)
191  if( ::ToDouble(y) == 0.)
192  return abs(x); //avoid roundof error from sqrt(x*x)
193  return PMReal(std::sqrt(::ToDouble(x*x + y*y)));
194  }
195 
201  PMReal VectorAngle() const;
202 
209  PMPoint MirrorPoint(const PMPoint& aligningPoint) const
210  { return PMReal(2.0) * aligningPoint - *this; }
211 
220  PMPoint ProjectOnLine(const PMPoint& pt0, const PMPoint &ptA) const;
221 
222  // Math Operators
223 
231  friend PMPoint operator +(const PMPoint& a, const PMPoint& b);
232 
240  friend PMPoint operator -(const PMPoint& a, const PMPoint& b);
241 
248  friend PMPoint operator -(const PMPoint& a);
249 
257  friend PMPoint operator *(const PMReal& c, const PMPoint& pt);
258 
266  friend PMPoint operator *(const PMPoint& pt, const PMReal& c);
267 
275  friend PMPoint operator *(const PMPoint& a, const PMPoint& b);
276 
284  friend PMPoint operator /(const PMPoint& numer, const PMPoint& denom);
285 
293  friend PMPoint operator /(const PMPoint& numer, const PMReal& denom);
294 
295  // Assignment Operators
296 
302  void operator +=(const PMPoint& a)
303  { x += a.x; y += a.y; }
304 
310  void operator -=(const PMPoint& a)
311  { x -= a.x; y -= a.y; }
312 
313  // Comparison Operators
314 
322  friend bool16 operator ==(const PMPoint& a, const PMPoint& b) noexcept;
323 
331  friend bool16 operator !=(const PMPoint& a, const PMPoint& b) noexcept;
332 
333 
338  bool operator<( const PMPoint& rhs) const noexcept
339  {
340  return x == rhs.x
341  ? y < rhs.y
342  : x < rhs.x;
343  }
344 
345 
346 
347  // Manipulation Methods
348 
355  bool16 ConstrainTo(const PMRect& r);
356 
360  void Round()
361  { x = ::Round(x); y = ::Round(y); }
362 
363  // Streaming
364 
371  void ReadWrite (IPMStream* iPMStream);
372 
373  private:
374  PMReal x, y;
375  friend class PMRect;
376 };
377 
378 constexpr PMPoint kZeroPoint(0, 0);
379 constexpr PMPoint kInvalidPoint(kPMMaxReal, kPMMaxReal);
380 
381 
382 
383 
384 //----------------------------------------------------------------------------------------
385 // operator +
386 //----------------------------------------------------------------------------------------
387 
388 inline PMPoint operator +(const PMPoint& a, const PMPoint& b)
389 {
390  return PMPoint(a.x + b.x, a.y + b.y);
391 }
392 
393 //----------------------------------------------------------------------------------------
394 // operator -
395 //----------------------------------------------------------------------------------------
396 
397 inline PMPoint operator -(const PMPoint& a, const PMPoint& b)
398 {
399  return PMPoint(a.x - b.x, a.y - b.y);
400 }
401 
402 //----------------------------------------------------------------------------------------
403 // operator -
404 //----------------------------------------------------------------------------------------
405 
406 inline PMPoint operator -(const PMPoint& a)
407 {
408  return PMPoint(-a.x, -a.y);
409 }
410 
411 //----------------------------------------------------------------------------------------
412 // operator *
413 //----------------------------------------------------------------------------------------
414 
415 inline PMPoint operator *(const PMReal& c, const PMPoint& pt)
416 {
417  return PMPoint(c * pt.x, c * pt.y);
418 }
419 
420 //----------------------------------------------------------------------------------------
421 // operator *
422 //----------------------------------------------------------------------------------------
423 
424 inline PMPoint operator *(const PMPoint& pt, const PMReal& c)
425 {
426  return PMPoint(c * pt.x, c * pt.y);
427 }
428 
429 //----------------------------------------------------------------------------------------
430 // operator *
431 //----------------------------------------------------------------------------------------
432 
433 inline PMPoint operator *(const PMPoint& a, const PMPoint& b)
434 {
435  return PMPoint(a.x * b.x, a.y * b.y);
436 }
437 
438 //----------------------------------------------------------------------------------------
439 // operator /
440 //----------------------------------------------------------------------------------------
441 
442 inline PMPoint operator /(const PMPoint& numer, const PMPoint& denom)
443 {
444  return PMPoint(numer.x/denom.x, numer.y/denom.y);
445 }
446 
447 //----------------------------------------------------------------------------------------
448 // operator /
449 //----------------------------------------------------------------------------------------
450 
451 inline PMPoint operator /(const PMPoint& numer, const PMReal& denom)
452 {
453  return PMPoint(numer.x/denom, numer.y/denom);
454 }
455 
456 
457 //----------------------------------------------------------------------------------------
458 // PMPoint::operator ==
459 //----------------------------------------------------------------------------------------
460 
461 inline bool16 operator ==(const PMPoint& a, const PMPoint& b) noexcept
462 {
463  return a.x == b.x && a.y == b.y;
464 }
465 
466 //----------------------------------------------------------------------------------------
467 // PMPoint::operator !=
468 //----------------------------------------------------------------------------------------
469 
470 inline bool16 operator !=(const PMPoint& a, const PMPoint& b) noexcept
471 {
472  return a.x != b.x || a.y != b.y;
473 }
474 
475 inline SysPoint ToSys(const PMPoint& pt)
476 {
477  SysPoint sysPt;
478 #if MACINTOSH
479  // Convert to the native coordinate system, but keep the coordinates integral
480  // (djb - this is assumed to happen in many places currently)
481  SysPointH(sysPt) = ::ToFloat(::Floor(pt.x));
482  SysPointV(sysPt) = ::ToFloat(::Floor(pt.y));
483 #else
484  SysPointH(sysPt) = ToInt32(pt.x);
485  SysPointV(sysPt) = ToInt32(pt.y);
486 #endif
487  return sysPt;
488 }
489 
490 
491 #endif