Guest User

Untitled

a guest
Jun 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <GL/glut.h>
  4. #include <stdlib.h>
  5. //Ukuran layar awal
  6. int HEIGHT = 400;
  7. int WIDTH = 400;
  8.  
  9. //Batas koordinat
  10. float X_MIN = -20.0;
  11. float X_MAX = 20.0;
  12. float Y_MIN = -20.0;
  13. float Y_MAX = 20.0;
  14.  
  15. void grid()
  16. {
  17. double i;
  18.  
  19. glColor3f(0.3,0.3,0.3);
  20. glBegin(GL_LINES);
  21. for(i=0;i<X_MAX;i++)
  22. {
  23. glVertex2f(i,Y_MAX);
  24. glVertex2f(i,Y_MIN);
  25. }
  26. for(i=0;i>X_MIN;i--)
  27. {
  28. glVertex2f(i,Y_MAX);
  29. glVertex2f(i,Y_MIN);
  30. }
  31. for(i=0;i<Y_MAX;i++)
  32. {
  33. glVertex2f(Y_MAX,i);
  34. glVertex2f(Y_MIN,i);
  35. }
  36. for(i=0;i>Y_MIN;i--)
  37. {
  38. glVertex2f(Y_MAX,i);
  39. glVertex2f(Y_MIN,i);
  40. }
  41. glEnd();
  42.  
  43. //Titik Pusat (0,0)
  44. glColor3f(1.0,1.0,1.0);
  45. glPointSize(5);
  46. glBegin(GL_POINTS);
  47. glVertex2f(0,0);
  48. glEnd();
  49. }
  50.  
  51. void spike(int n,float size){
  52. float sudut=(2*3.14)/n;
  53. float x,y;
  54. int i;
  55.  
  56. glPointSize(5);
  57. glLineWidth(2);
  58.  
  59. for(i=0;i<n;i++){
  60. //glColor3f((float) i/n,(float) i/n,(float) i/n);
  61. glBegin(GL_LINES);
  62. x=sin(i*sudut) * size;
  63. y=cos(i*sudut) * size;
  64. glVertex2f(0.0,0.0);
  65. glVertex2f(x,y);
  66. }
  67. glEnd();
  68. }
  69.  
  70. void bintang(int n,float size){
  71. float sudut=(2*3.14)/n;
  72. float x,y;
  73. int i;
  74.  
  75. glPointSize(5);
  76. glLineWidth(2);
  77.  
  78. glBegin(GL_LINE_LOOP);
  79. //verterx ganjil
  80. for(i=1;i<=n;i+=2){
  81. x=sin(i*sudut) * size;
  82. y=cos(i*sudut) * size;
  83. glVertex2f(x,y);
  84. }
  85.  
  86. //vertex genap
  87. for(i=2;i<=n;i+=2){
  88. x=sin(i*sudut) * size;
  89. y=cos(i*sudut) * size;
  90. glVertex2f(x,y);
  91. }
  92.  
  93. glEnd();
  94. }
  95.  
  96. void draw_pixel(float x, float y) {
  97.  
  98. glColor3f(1.0, 0.0, 0.0);
  99. glBegin(GL_POLYGON);
  100. glVertex2f(x, y);
  101. glVertex2f(x+1, y);
  102. glVertex2f(x+1, y+1);
  103. glVertex2f(x, y+1);
  104. glEnd();
  105.  
  106. }
  107.  
  108. void naive(int x1, int y1, int x2, int y2)
  109. {
  110. int dx = x2 - x1;
  111. int dy = y2 - y1;
  112.  
  113. draw_pixel(x1,y1);
  114. if(dx != 0) {
  115. float m = (float) dy / (float) dx;
  116. float b = y1 - m*x1;
  117. dx = (x2 > x1) ? 1 : -1;
  118. while (x1 != x2) {
  119. x1 += dx;
  120. y1 = (int)(m*x1 + b);
  121. draw_pixel(x1,y1);
  122. }
  123. }
  124. }
  125.  
  126.  
  127. void dda(int x1, int y1, int x2, int y2)
  128. {
  129. int dy = y2 - y1;
  130. int dx = x2 - x1;
  131. float t = (float) 0.5;
  132.  
  133. draw_pixel(x1,y1);
  134. if (abs(dx) > abs(dy)) {
  135. float m = (float) dy / (float) dx;
  136. t += y1;
  137. dx = (dx<0) ? -1 : 1;
  138. m *= dx;
  139. while (x1 != x2){
  140. x1 += dx;
  141. t += m;
  142. draw_pixel(x1, (int) t);
  143. }
  144. }
  145. else {
  146. float m = (float) dx / (float) dy;
  147. t += x1;
  148. dy = (dy<0) ? -1 : 1;
  149. m *= dy;
  150. while( y1 != y2) {
  151. y1 += dy;
  152. t += m;
  153. draw_pixel((int)t,y1);
  154. }
  155. }
  156. }
  157.  
  158. void BresenhamLine (int x0,int y0,int xend, int yend,int color)
  159. { int dx,dy, incre_p1, incre_p2, p, x, y;
  160. dy=yend-y0, dx=xend-x0;
  161. incre_p1=2*dy, incre_p2=2* (dy-dx);
  162. x=x0, y=y0;
  163. p=2*dy-dx;
  164. drawpixel(x, y, color);
  165. while (x<x1)
  166. { if (p<0) {x++, p+=incre_d1; }
  167. else {x++, y++,p+=incre_d2;}
  168. drawpixel (x, y, color);
  169. } /* while */
  170. } /* Bresenham */
  171.  
  172.  
  173. void display(){
  174. glClear(GL_COLOR_BUFFER_BIT);
  175. grid();
  176.  
  177. //spike(5,10);
  178. //bintang(5,10);
  179. //naive(0,0,5,5);
  180.  
  181. glFlush();
  182. }
  183.  
  184. void init() {
  185. glClearColor( 0.0, 0.0, 0.0, 0.0);
  186. glMatrixMode(GL_PROJECTION);
  187. glLoadIdentity();
  188. gluOrtho2D(X_MIN,X_MAX,Y_MIN,Y_MAX);
  189. glMatrixMode(GL_MODELVIEW);
  190. }
  191.  
  192.  
  193. int main( int argc, char **argv) {
  194. glutInit( &argc, argv);
  195. glutInitWindowSize(WIDTH, HEIGHT);
  196. glutInitWindowPosition(0, 0);
  197. glutCreateWindow("Segi-N Beraturan");
  198.  
  199. glutDisplayFunc(display);
  200.  
  201.  
  202. glEnable(GL_POINT_SMOOTH);
  203. glEnable(GL_LINE_SMOOTH);
  204. glEnable(GL_BLEND); //memperhalus garis
  205. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // memperhalus garis
  206.  
  207. init();
  208. glutMainLoop();
  209. return 0;
  210. }
Add Comment
Please, Sign In to add comment