Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <windows.h>
  5. #include <math.h>
  6.  
  7. typedef struct{
  8. float x, y;
  9. } point2D_t;
  10.  
  11. typedef struct{
  12. float r, g, b;
  13. } color_t;
  14.  
  15. typedef struct{
  16. float v[3];
  17. } vector2D_t;
  18.  
  19. typedef struct{
  20. float m[3][3];
  21. } matrix2D_t;
  22.  
  23. vector2D_t point2vector(point2D_t p){
  24. vector2D_t vec;
  25. vec.v[0]=p.x;
  26. vec.v[1]=p.y;
  27. vec.v[2]=1;
  28. return vec;
  29. }
  30.  
  31. point2D_t vector2point(vector2D_t vec){
  32. point2D_t p;
  33. p.x=vec.v[0];
  34. p.y=vec.v[1];
  35. return p;
  36. }
  37.  
  38. vector2D_t operator * (matrix2D_t mat, vector2D_t vec){
  39. vector2D_t vec1;
  40. for(int i=0;i<3;i++){
  41. vec1.v[i]=0;
  42. for(int j=0;j<3;j++){
  43. vec1.v[i]+=mat.m[i][j]*vec.v[j];
  44. }
  45. }
  46. return vec1;
  47. }
  48.  
  49. matrix2D_t operator * (matrix2D_t mat1, matrix2D_t mat2){
  50. matrix2D_t mat;
  51. for(int i=0;i<3;i++){
  52. for(int j=0;j<3;j++){
  53. mat.m[i][j]=0;
  54. for(int k=0;k<3;k++){
  55. mat.m[i][j]+=mat1.m[i][k]*mat2.m[k][j];
  56. }
  57. }
  58. }
  59. return mat;
  60. }
  61.  
  62. matrix2D_t createIdentity(){
  63. matrix2D_t mat;
  64. for(int i=0;i<3;i++){
  65. for(int j=0;j<3;j++){
  66. mat.m[i][j]=0;
  67. }
  68. mat.m[i][i]=1;
  69. }
  70. return mat;
  71. }
  72.  
  73. matrix2D_t translationMTX(float dx, float dy){
  74. matrix2D_t mat=createIdentity();
  75. mat.m[0][2]=dx;
  76. mat.m[1][2]=dy;
  77. return mat;
  78. }
  79.  
  80. matrix2D_t scalingMTX(float sx, float sy){
  81. matrix2D_t mat=createIdentity();
  82. mat.m[0][0]=sx;
  83. mat.m[1][1]=sy;
  84. return mat;
  85. }
  86.  
  87. matrix2D_t rotationMTX(float a){
  88. matrix2D_t mat=createIdentity();
  89. mat.m[0][0]=cos(a);
  90. mat.m[0][1]=-sin(a);
  91. mat.m[1][0]=sin(a);
  92. mat.m[1][1]=cos(a);
  93. return mat;
  94. }
  95.  
  96. void drawPolygon(point2D_t p[], int n){
  97. glBegin(GL_POLYGON);
  98. for(int i=0; i<n; i++){
  99. glVertex2f(p[i].x,p[i].y);
  100. }
  101. glEnd();
  102. }
  103.  
  104. void drawPolyline(point2D_t p[], int n){
  105. glBegin(GL_LINE_LOOP);
  106. for(int i=0; i<n; i++){
  107. glVertex2f(p[i].x,p[i].y);
  108. }
  109. glEnd();
  110. }
  111.  
  112. void drawPolygon(point2D_t p[], color_t col, int n){
  113. glColor3f(col.r, col.g, col.b);
  114. glBegin(GL_POLYGON);
  115. for(int i=0; i<n; i++){
  116. glVertex2f(p[i].x,p[i].y);
  117. }
  118. glEnd();
  119. }
  120.  
  121. void drawPolygon(point2D_t p[], color_t col[], int n){
  122. glBegin(GL_POLYGON);
  123. for(int i=0; i<n; i++){
  124. glColor3f(col[i].r, col[i].g, col[i].b);
  125. glVertex2f(p[i].x,p[i].y);
  126. }
  127. glEnd();
  128. }
  129.  
  130. void drawCircle(float r, float xp, float yp, int n){
  131. point2D_t p[360];
  132. float teta;
  133. for(int i=0; i<n; i++){
  134. teta=(float)i*6.28/n;
  135. p[i].x=xp+r*cos(teta);
  136. p[i].y=yp+r*sin(teta);
  137. }
  138. drawPolygon(p,n);
  139. }
  140. void drawLine(float x1, float y1, float x2, float y2){
  141. glBegin(GL_LINES);
  142. glVertex2f(x1,y1);
  143. glVertex2f(x2,y2);
  144. glEnd();
  145. }
  146.  
  147. typedef struct {
  148. int n;
  149. point2D_t p[100];
  150. } object2D_t;
  151.  
  152. object2D_t createCircle(float r, int n){
  153. object2D_t obj;
  154. obj.n=n;
  155. for(int i=0;i<n;i++){
  156. float s=i*6.28/n;
  157. obj.p[i].x=r*cos(s);
  158. obj.p[i].y=r*sin(s);
  159. }
  160. return obj;
  161. }
  162.  
  163. void userdraw(void) {
  164. glLineWidth(2);
  165. for(int i=0; i<100; i+=5){
  166. /* drawLine(i,0,100,i);
  167. drawLine(100,i,100-i,100);
  168. drawLine(100-i,100,0,100-i);
  169. drawLine(0,100-i,i,0);
  170. point2D_t p[4]={{i,0},{100,i},{100-i,100},{0,100-i}};
  171. drawPolyline(p,4);
  172. */
  173.  
  174. float x1,y1,x2,y2;
  175. float a,b;
  176. for(int i=0; i<360; i+=10){
  177. a=i/57.3;
  178. b=(i+120)/57.3;
  179. x1=50+50*cos(a);
  180. y1=50+50*sin(a);
  181. x2=50+50*cos(b);
  182. y2=50+50*sin(b);
  183. drawLine(x1,y1,x2,y2);
  184. }
  185. for(int i=0; i<360; i+=10){
  186. a=i/57.3;
  187. b=(i+120)/57.3;
  188. x1=50+25*cos(a);
  189. y1=50+25*sin(a);
  190. x2=50+25*cos(b);
  191. y2=50+25*sin(b);
  192. drawLine(x1,y1,x2,y2);
  193.  
  194.  
  195. }
  196. }
  197. }
  198.  
  199. void display(void) {
  200. glClear( GL_COLOR_BUFFER_BIT);
  201. userdraw();
  202. glutSwapBuffers();
  203. }
  204.  
  205. int main(int argc, char **argv){
  206. glutInit(&argc,argv);
  207. glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB );
  208. glutInitWindowPosition(100,100);
  209. glutInitWindowSize(600,600);
  210. glutCreateWindow ("Drawing by Achmad Basuki");
  211. glClearColor(0.0, 0.0, 0.0, 0.0);
  212. gluOrtho2D(0, 100, 0, 100);
  213. glutIdleFunc(display);
  214. glutDisplayFunc(display);
  215. glutMainLoop();
  216.  
  217. return 0;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement