fork download
  1. #include <stdio.h>
  2.  
  3. #define SCREEN_HEIGHT 22
  4. #define SCREEN_WIDTH 78
  5.  
  6. // Simulated frame buffer
  7. char Screen[SCREEN_HEIGHT][SCREEN_WIDTH];
  8.  
  9. void SetPixel(int x, int y, char color)
  10. {
  11. if ((x < 0) || (x >= SCREEN_WIDTH) ||
  12. (y < 0) || (y >= SCREEN_HEIGHT))
  13. return;
  14.  
  15. Screen[y][x] = color;
  16. }
  17.  
  18. void Visualize(void)
  19. {
  20. int x, y;
  21.  
  22. for (y = 0; y < SCREEN_HEIGHT; y++)
  23. {
  24. for (x = 0; x < SCREEN_WIDTH; x++)
  25. printf("%c", Screen[y][x]);
  26.  
  27. printf("\n");
  28. }
  29. }
  30.  
  31. typedef struct
  32. {
  33. int x, y;
  34. } Point2D;
  35.  
  36. int main(void)
  37. {
  38. // triangle vertices
  39. Point2D vx0 = { SCREEN_WIDTH / 2, SCREEN_HEIGHT / 7 };
  40. Point2D vx1 = { SCREEN_WIDTH * 6 / 7, SCREEN_HEIGHT * 2 / 3 };
  41. Point2D vx2 = { SCREEN_WIDTH / 7, SCREEN_HEIGHT * 6 / 7 };
  42. // vectors lying on triangle sides
  43. Point2D v0, v1, v2;
  44. // current point coordinates
  45. int x, y;
  46.  
  47. // calculate side vectors
  48.  
  49. v0.x = vx1.x - vx0.x;
  50. v0.y = vx1.y - vx0.y;
  51.  
  52. v1.x = vx2.x - vx1.x;
  53. v1.y = vx2.y - vx1.y;
  54.  
  55. v2.x = vx0.x - vx2.x;
  56. v2.y = vx0.y - vx2.y;
  57.  
  58. // process all points
  59.  
  60. for (y = 0; y < SCREEN_HEIGHT; y++)
  61. for (x = 0; x < SCREEN_WIDTH; x++)
  62. {
  63. int z1 = (x - vx0.x) * v0.y - (y - vx0.y) * v0.x;
  64. int z2 = (x - vx1.x) * v1.y - (y - vx1.y) * v1.x;
  65. int z3 = (x - vx2.x) * v2.y - (y - vx2.y) * v2.x;
  66.  
  67. if ((z1 * z2 > 0) && (z1 * z3 > 0))
  68. SetPixel(x, y, '+'); // point is to the right (left) of all vectors
  69. else
  70. SetPixel(x, y, '-');
  71. }
  72.  
  73. // draw triangle vertices
  74.  
  75. SetPixel(vx0.x, vx0.y, '0');
  76. SetPixel(vx1.x, vx1.y, '1');
  77. SetPixel(vx2.x, vx2.y, '2');
  78.  
  79. // visualize the result
  80.  
  81. Visualize();
  82.  
  83. return 0;
  84. }
  85.  
Success #stdin #stdout 0s 1788KB
stdin
Standard input is empty
stdout
------------------------------------------------------------------------------
------------------------------------------------------------------------------
------------------------------------------------------------------------------
---------------------------------------0--------------------------------------
--------------------------------------++++------------------------------------
------------------------------------++++++++----------------------------------
----------------------------------+++++++++++++-------------------------------
--------------------------------+++++++++++++++++-----------------------------
------------------------------++++++++++++++++++++++--------------------------
----------------------------++++++++++++++++++++++++++------------------------
--------------------------+++++++++++++++++++++++++++++++---------------------
-------------------------++++++++++++++++++++++++++++++++++-------------------
-----------------------+++++++++++++++++++++++++++++++++++++++----------------
---------------------+++++++++++++++++++++++++++++++++++++++++++--------------
-------------------+++++++++++++++++++++++++++++++++++++++++++++++1-----------
-----------------++++++++++++++++++++++++++++++++++++-------------------------
---------------++++++++++++++++++++++++---------------------------------------
-------------++++++++++++-----------------------------------------------------
-----------2------------------------------------------------------------------
------------------------------------------------------------------------------
------------------------------------------------------------------------------
------------------------------------------------------------------------------