line.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // This function should draw 2d and 3d lines, and just keep getting better.
  19. #include <stdlib.h>
  20. #include "System.h"
  21. #ifdef PATHS_IN_INCLUDES
  22. #include "GREEN/BLiT/BLIT.H"
  23. //#include <math.h>
  24. //#include "numbers.h" // allow
  25. //#include "_QuickTrig.h"
  26. //#include "_line.h"
  27. #include "ORANGE/QuickMath/QuickMath.h"
  28. #include "ORANGE/QuickMath/FixedPoint.h"
  29. #include "ORANGE/QuickMath/Fractions.h"
  30. #else
  31. #include "BLIT.H"
  32. //#include <math.h>
  33. //#include "numbers.h" // allow
  34. //#include "_QuickTrig.h"
  35. //#include "_line.h"
  36. #include "QuickMath.h"
  37. #include "FixedPoint.h"
  38. #include "Fractions.h"
  39. #endif
  40. #ifdef MOBILE //Arm RAND_MAX is a full int, code expecting a short!!
  41. #define RAND_MAX 0x7fff
  42. #endif
  43. // Here's a very quick rip off!
  44. // I don't even know if it's 2 way consistent!
  45. // (It sure doesn't clip!)
  46. // VERY cheezy implementation! For drawing, should do IC stuff inline with pDst
  47. //
  48. void rspLine(UCHAR ucColor,RImage* pimDst,short sX1,short sY1,short sX2,short sY2,const RRect* prClip)
  49. {
  50. /*
  51. #ifdef _DEBUG
  52. if ((sX1 < 0) || (sY1 < 0) || (sX2 >= pimDst->lWidth) || (sY2 >= pimDst->lHeight))
  53. {
  54. TRACE("line: Error... Clipped line!\n");
  55. return;
  56. }
  57. #endif
  58. */
  59. // use the cheap 3D technique with signed fractions:
  60. short sDelX = sX2 - sX1; // signed
  61. short sDelY = sY2 - sY1; // signed
  62. short sDelZ = MAX(ABS(sDelX),ABS(sDelY)); // a slow trick for now...
  63. if (sDelZ == 0) // a single point
  64. {
  65. rspClipPlot(ucColor,pimDst,sX1,sY1,prClip);
  66. return;
  67. }
  68. RFracS16 frIncX,frIncY;
  69. rspfrDiv(frIncX,sDelX,sDelZ); // Magnitude < 1
  70. rspfrDiv(frIncY,sDelY,sDelZ); // Magnitude < 1
  71. RFracS16 frX,frY;
  72. frX.mod = sX1; frY.mod = sY1;
  73. frX.frac = frY.frac = (sDelZ >> 1); // for pixel rounding
  74. for (short i=0;i < sDelZ;i++)
  75. {
  76. rspClipPlot(ucColor,pimDst,frX.mod,frY.mod,prClip);
  77. rspfrAdd(frX,frIncX,sDelZ);
  78. rspfrAdd(frY,frIncY,sDelZ);
  79. }
  80. }
  81. // returns a short random number between 0 and N-1
  82. //
  83. short rspRand(short sMax)
  84. {
  85. return (short)((rand() * sMax) / RAND_MAX);
  86. }