Advertisement
Guest User

DrawNaiveLine

a guest
Jan 23rd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. void DrawNaiveLine(const Point &p1, const Point &p2, unsigned char r, unsigned char g, unsigned char b)
  2. {
  3. //test if the line is vertical
  4. if (Round(p1.x) == Round(p2.x)) DrawVerticalLine(p1, p2, r, g, b);
  5. //test if the line is horizontal
  6. else if (Round(p1.y) == Round(p2.y)) DrawHorizontalLine(p1, p2, r, g, b);
  7. //test if the line is diagonal
  8. else if (abs(Round(p1.x) - Round(p2.x)) == abs(Round(p1.y) - Round(p2.y)))
  9. DrawDiagonalLine(p1, p2, r, g, b);
  10. //otherwise the line is not simple
  11. else
  12. {
  13. if (DEBUG) printf("Drawing non-trivial line from p1(%3.3f, %3.3f) to p2(%3.3f, %3.3f)\n", p1.x, p1.y, p2.x, p2.y);
  14. //calculate slope
  15. //(no danger of divison by zero due to vertical line test)
  16. float m = (p2.y - p1.y) / (p2.x - p1.x);
  17. //calculate intercept
  18. float b_ = p1.y - (m*p1.x);
  19.  
  20. //step through the y
  21. if (abs(m) > 1)
  22. {
  23. //starting with p1
  24. if (p1.y < p2.y)
  25. {
  26. if (DEBUG) printf("stepping along the y axis starting with p1\n");
  27.  
  28. //calculate the distance
  29. float distance = p2.y - p1.y;
  30.  
  31. //draw the line
  32. for (int i = 0; i < distance + 1; ++i)
  33. {
  34. //calculate the x value
  35. float x = m*(p1.y + i) + b_;
  36.  
  37. //draw the pixel
  38. if (DEBUG) printf("drawing pixel at (%d, %d)\n",
  39. Round(x), Round(p1.y + i));
  40. FrameBuffer::SetPixel(Round(x), Round(p1.y + i), r, g, b);
  41. }
  42. }
  43. //starting with p2
  44. else
  45. {
  46. if (DEBUG) printf("stepping along the y axis starting with p2\n");
  47. //calculate the distance
  48. float distance = p1.y - p2.y;
  49.  
  50. //draw the line
  51. for (int i = 0; i < distance + 1; ++i)
  52. {
  53. //calculate the x value
  54. float x = m*(p2.y + i) + b_;
  55.  
  56. //draw the pixel
  57. if (DEBUG) printf("drawing pxel at (%d, %d)\n",
  58. Round(x), Round(p2.y + i));
  59. FrameBuffer::SetPixel(Round(x), Round(p2.y + i), r, g, b);
  60. }
  61. }
  62. }
  63. //step through the x
  64. else if (abs(m) < 1)
  65. {
  66. //starting with p1
  67. if (p1.x < p2.x)
  68. {
  69. if (DEBUG) printf("stepping along the x axis starting with p1\n");
  70.  
  71. //calculate the distance
  72. float distance = p2.x - p1.x;
  73.  
  74. //draw the line
  75. for (int i = 0; i < distance + 1; ++i)
  76. {
  77. //calculate the y value
  78. float y = m*(p1.x + i) + b_;
  79.  
  80. //draw the pixel
  81. if (DEBUG) printf("drawing pixel at (%d, %d)\n",
  82. Round(p1.x+i), Round(y));
  83. FrameBuffer::SetPixel(Round(p1.x + i), Round(y), r, g, b);
  84. }
  85. }
  86. //starting with p2
  87. else
  88. {
  89. if (DEBUG) printf("stepping along the x axis starting with p2\n");
  90. //calculate the distance
  91. float distance = p1.x - p2.x;
  92.  
  93. //draw the line
  94. for (int i = 0; i < distance + 1; ++i)
  95. {
  96. //calculate the x value
  97. float y = m*(p2.x + i) + b_;
  98.  
  99. //draw the pixel
  100. if (DEBUG) printf("drawing pxel at (%d, %d)\n",
  101. Round(p2.x+i), Round(y));
  102. FrameBuffer::SetPixel(Round(p2.x + i), Round(y), r, g, b);
  103. }
  104. }
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement