DiscreteCircle.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: DiscreteCircle.cpp ////////////////////////////////////////////////////
  24. //-----------------------------------------------------------------------------
  25. //
  26. // EA Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2002 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // Project: RTS3
  34. //
  35. // File name: DiscreteCircle.cpp
  36. //
  37. // Created: John McDonald, September 2002
  38. //
  39. // Desc: ???
  40. //
  41. //-----------------------------------------------------------------------------
  42. #include "PreRTS.h"
  43. #include "Common/DiscreteCircle.h"
  44. //-------------------------------------------------------------------------------------------------
  45. DiscreteCircle::DiscreteCircle(Int xCenter, Int yCenter, Int radius)
  46. {
  47. m_yPos = yCenter;
  48. m_yPosDoubled = (yCenter << 1);
  49. m_edges.reserve(radius << 1); // largest that it should ever be.
  50. generateEdgePairs(xCenter, yCenter, radius);
  51. removeDuplicates();
  52. }
  53. //-------------------------------------------------------------------------------------------------
  54. void DiscreteCircle::drawCircle(ScanlineDrawFunc functionToDrawWith, void *parmToPass)
  55. {
  56. for (VecHorzLine::const_iterator it = m_edges.begin(); it != m_edges.end(); ++it) {
  57. (functionToDrawWith)(it->xStart, it->xEnd, it->yPos, parmToPass);
  58. if (it->yPos != m_yPos) {
  59. (functionToDrawWith)(it->xStart, it->xEnd, m_yPosDoubled - it->yPos, parmToPass);
  60. }
  61. }
  62. }
  63. //-------------------------------------------------------------------------------------------------
  64. void DiscreteCircle::generateEdgePairs(Int xCenter, Int yCenter, Int radius)
  65. {
  66. // Uses Bresenham to generate points.
  67. Int x = 0;
  68. Int y = radius;
  69. Int d = (1 - radius) << 1;
  70. while (y >= 0) {
  71. HorzLine hl;
  72. hl.xStart = xCenter - x;
  73. hl.xEnd = xCenter + x;
  74. hl.yPos = yCenter + y;
  75. m_edges.push_back(hl);
  76. if (d + y > 0) {
  77. --y;
  78. d -= ((y << 1) - 1);
  79. }
  80. if (x > d) {
  81. ++x;
  82. d += ((x << 1) + 1);
  83. }
  84. }
  85. }
  86. //-------------------------------------------------------------------------------------------------
  87. void DiscreteCircle::removeDuplicates()
  88. {
  89. VecHorzLineIt it, nextIt;
  90. for ( it = m_edges.begin(); it != m_edges.end(); /* empty */) {
  91. nextIt = it;
  92. ++nextIt;
  93. if (nextIt == m_edges.end()) {
  94. break;
  95. }
  96. if (it->yPos == nextIt->yPos) {
  97. it = m_edges.erase(it);
  98. } else {
  99. ++it;
  100. }
  101. }
  102. }