file polygon.h

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 1
Reputation: roidel is an unknown quantity at this point 
Solved Threads: 0
roidel roidel is offline Offline
Newbie Poster

file polygon.h

 
0
  #1
Dec 5th, 2008
Hi, i found this code from an article, i want to compile this code, buat I don't have file polygon.h. Can you send file polygon.h to me, because i have search from any web buat i don't find it.

This the code


  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<windows.h>
  4. #include<math.h>
  5. #include<stdlib.h>
  6. #include<GL/glut.h>
  7.  
  8.  
  9. #define SCREEN_W 800
  10. #define SCREEN_H 600
  11. #define MAX_INPUT 20
  12. #define MAX_POLYGON 10
  13. #define MAX_SINAR 150
  14.  
  15. #define BELUM_MEMBUAT_POLYGON -1
  16. #define MEMBUAT_POLYGON 1
  17. #define SIAP_BUAT_POLYGON 0
  18. #define SELESAI_BUAT_POLUGON 2
  19. #define KLIK_TITIK_P1 3
  20. #define CARI_SINAR 4
  21.  
  22. #include"vector.h"
  23. #include"polygon.h"
  24.  
  25. int status = SIAP_BUAT_POLYGON;
  26. Poly polygon[MAX_POLYGON];
  27. int jumlahPolygon = 0;
  28. int jumlahSinar = 0;
  29. vector p,p1;
  30. vector sinar[MAX_SINAR];
  31. int hitTime[MAX_POLYGON];
  32. vector currentSinar;
  33. int lokasiPantul[2];
  34.  
  35. void printPolygon()
  36. {
  37.  
  38. // membuat garis loop / buat polygon
  39. glColor3f(0.0f, 0.0f, 0.0f);
  40. for (int 1=0; i<jumlahPolygon; i++)
  41. {
  42. glBegin(GL_LINE_LOOP);
  43. for (int j=0; j<polygon[i].getJumlahInput(); j++)
  44. {
  45. glVertex2d(polygon[i].get(j).getX(), polygon[i].get(j).getY());
  46. }
  47. glEnd();
  48. }
  49. glFlush();
  50.  
  51. }
  52.  
  53. void printPoint()
  54. {
  55. // buat titik
  56. glColor3f(0.0f, 0.0f, 0.0f);
  57. glBegin(GL_POINTS);
  58. for (int i=0; i<jumlahPolygon; i++)
  59. {
  60. for (int j=0; j<polygon[i].getJumlahInput(); j++)
  61. {
  62. glVertex2d(polygon[i].get(j).getX(), polygon[i].get(j).getY());
  63. }
  64. }
  65. glColor3f(1, 0.0f, 1);
  66. glVertex2d(p.getX(), p.getY());
  67. glEnd();
  68. glflush();
  69. }
  70.  
  71. void printSinar()
  72. {
  73. glColor3f(1, 0.0f, 1);
  74. glBegin(GL_LINES);
  75. for (int i=1; i<jumlahSinar; i++)
  76. {
  77. glBegin(GL_LINES);
  78. glVertex2d(sinar[i-1].getX(), sinar[i-1].getY());
  79. glVertex2d(sinar[i].getX(), sinar[i].getY());
  80. glEnd();
  81. }
  82. glFlush();
  83. }
  84.  
  85. double cariUa(vector VR, vector titikAwal, vector titikTembok1, vector titikTembok2)
  86. {
  87. vektor titikToTembok;
  88. vector tembok;
  89.  
  90. titikToTembok.set(titikTembok1.getX()-titikAwal.getX(), titikTembok1.getY()-titikAwal.getY(),0);
  91. tembok.set(titikTembok2.getX()-titikTembok1.getX(), titikTembok2.getY()-titikTembok1.getY(),0);
  92.  
  93. vector V;
  94.  
  95. double Ua=V.crossProduct(VR, titikToTembok).getZ()/V.crossProduct(tembok,VR).getZ();
  96.  
  97. return Ua;
  98. }
  99.  
  100. double getHitTime(vector VR, vector titikAwal, vector titikTembok1, vector titikTembok2)
  101. {
  102. vector titikToTembok;
  103. vector Tembok;
  104.  
  105. titikToTembok.set(titikTembok1.getX()-titikAwal.getX(), titikTembok1.getY()-titikAwal.getY(),0);
  106. tembok.set(titikTembok2.getX()-titikTembok1.getX(), titikTembok2.getY()-titikTembok1.getY(),0);
  107.  
  108. Vector V;
  109.  
  110. double Ua=V.crossProduct(tembok, titikToTembok).getZ()/V.crossProduct(tembok,VR).getZ();
  111.  
  112. return hitTime;
  113. }
  114.  
  115. vector getTitikPantul(vector tembok, vector sinar, double hitTime)
  116. {
  117. vector pantul;
  118. pantul.set(tembok.getX()+hitTime*sinar.getX(), tembok.getY()+hitTime*sinar.getT(),0);
  119. return pantul;
  120. }
  121.  
  122. vector getVectorPantul(vector sinar, vector normal)
  123. {
  124. vector pantul;
  125. vector V;
  126. double dotProductX2=2*V.dotProduct(sinar,normal);
  127. pantul.set(sinar.getX()-dotProductX2*normal.getX(), sinar.getY()-dotProductX2*normal.getY());
  128. return pantul;
  129. }
  130.  
  131. vector getSinar()
  132. {
  133. // cari tembok yang menabrak dan cari hitTimenya
  134. double Ua;
  135. double minHitTime = -1;
  136. int lokasiTembok [2];
  137.  
  138. for (int i=0; i<julahPolygon; i++)
  139. {
  140. for (int j=0; j<polygon[i].getJumlahInput(); j++)
  141. {
  142. if (i==lokasiPantul[0] && j++lokasiPantul[1])continue;
  143. Ua = cariUa(currentSinar, sinar[jumlahSinar-1], polygon[i].get(j), polygon[i].get((j+1)%polygon[i].getJumlahInput()));
  144. if (Ua>0 && Ua<=1)
  145. {
  146. // cari hitTime
  147. double hitTime=getHitTime(currentSinar,sinar[jumlahSinar-1],polygon[i].get(j), polygon[i].get((j+1)%polygon[i].getJumlahInput()));
  148. // jika bertabrakan
  149. if (hitTime>0)
  150. {
  151. if (minHitTime==-1)
  152. {
  153. minHitTime=hitTime;
  154. lokasiTembok[0]=i;
  155. lokasiTembok[1]=j;
  156. }
  157. else if (hitTime < minHitTime)
  158. {
  159. minHitTime=hitTime;
  160. lokasiTembok[0]=i;
  161. lokasiTembok[1]=j;
  162. }
  163. }
  164. }
  165. }
  166. }
  167. lokasiPantul[0]=lokasiTembok[0];
  168. lokasiPantul[1]=lokasiTembok[1];
  169. printf("tembok pantulan : %d,%d\n",lokasiTembok[0],lokasiTembok[1]);
  170. // tembok yang kena
  171. vector tembok = polygon[lokasiTembok[0]].getLine(lokasiTembok[1]);
  172.  
  173. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  174.  
  175. /** cari titik pantul */
  176.  
  177. vektor titikPantul = getTitikPantul(sinar[jumlahSinar-1),currentSinar,minHitTime);
  178. printf(Titik pantul=%3lf,%3lf ::
  179.  
  180. HitTime=%.3lf\n",titikPantul.getX(),titikPantul.getY(),minHitTime);
  181. /** Tambah Sinar */
  182.  
  183. sinar[jumlahSinar++].set(titikPantul.getX(),titikPantul.getY(),0);
  184.  
  185. /** vektor normal dititik pantul */
  186.  
  187. Vektor V;
  188.  
  189. vektor perp;
  190. perp.set(tembok.getY()*-1,tembok.getX(),0);
  191.  
  192. if(V.dotProduct(polygon[lokasiTEmbok[0]].get((lokasiTembok[1]+1)%polygon[lokasiTembok[0]].getJumlahInput()),perp)==-1)
  193. {
  194. {
  195. /** negatifkan */
  196.  
  197. perp.set(perp.getX()*-1,perp.getY()*-1,0);
  198.  
  199. }
  200.  
  201. double panjang = perp.getPanjang();
  202. vektor normal = perp.getNormal();
  203. vektor nextSinar = getVektorPantul(currentSinar,normal);
  204. return nextSinar;
  205.  
  206. }
  207.  
  208. /** Mouse handle Function */
  209.  
  210. void mouse(int button, int state, int x, int y){
  211.  
  212. switch(button){
  213. case GLUT_LEFT_BUTTON:
  214. /** onRelease, klik untuk menentukan titik2 polygon */
  215.  
  216. if(state==GLUT_UP && status < SELESAI_BUAT_POLYGON){
  217. status = MEMBUAT_POLYGON;
  218. if(polygon[jumlahPolygon].getJumlahInput()==MAX_INPUT)
  219. {
  220. printf();
  221. break;
  222. }
  223.  
  224. polygon[jumlahPolygon].add(x,SCREEN_H-y,0);
  225.  
  226. }
  227. /** mengklik titik(Point P) yang dicari posisinya */
  228.  
  229. else if(state == GLUT_UP && status == SELESAI_BUAT_POLYGON){
  230. print("klik titik P\n");
  231. p.set(x,SCREEN_H - y,0);
  232. status = KLIK_TITIK_P1;
  233. }
  234.  
  235. /** Mengklik titik(Point P) yang dicari posisinya */
  236.  
  237. else if(state == GLUT_UP && status == KLIK_TITIK_P1) {
  238. printf("Klik titik P1\n");
  239. p1.set(x,SCREEN_H-y,0;
  240.  
  241. status = CARI_SINAR;
  242. sinar[jumlahSinar++].set(p.getX(),p.getY(),p.getZ());
  243.  
  244. currentSinar.set(p1.getX() - p.getX(), p1.getY(),0);
  245.  
  246. }
  247.  
  248. break;
  249.  
  250. case GLUT_RIGHT_BUTTON;
  251.  
  252. /** Mengakhiri pembuatan titik2 untuk satu polygon */
  253.  
  254. if(state == GLUT_UP && status == MEMBUAT_POLYGON){
  255. jumlahPolygon +=1;
  256.  
  257. status = SIAP_BUAT_POLYGON;
  258.  
  259. }
  260.  
  261. /* Mengakhiri pembuatan polygon2' */
  262. else if(state == GLUT_UP && status == SIAP_BUAT_POLYGON){
  263. status = SELESAI_BUAT_POLYGON;
  264. }
  265.  
  266. }
  267.  
  268. }
  269. /* display function */
  270.  
  271. void display(void){
  272. if(jumlahSinar == MAX_SINAR)return;
  273. vektor next;
  274.  
  275. if(status == CARI_SINAR)
  276. {
  277. next = getSinar();
  278. currentSinar.set(next.getX(),next.getY(),next.getZ());
  279.  
  280. }
  281. glClear(GL_COLOR_BUFFER_BIT);
  282.  
  283. glColor3f(0.0,0.0,1.0);
  284.  
  285. printPolygon();
  286. printPoint();
  287. printSinar();
  288.  
  289. glEnd();
  290. glFlush();
  291. }
  292.  
  293. void myInit(void){
  294. glClearColor(1.0,1.0,1.0,0.0);
  295. glColor3f(0.0f,0.0f,0.0f);
  296. glPointSize(5);
  297. glMatrixModel(GL_POJECTION);
  298. glLoadIdentity();
  299. glOrtho2D(0.0,SCREEN_W,0.0,SCREEN_H);
  300.  
  301. }
  302.  
  303. void idle(void){
  304.  
  305. glutPostRedisplay();
  306.  
  307. }
  308.  
  309. int main(int argc, char **argv){
  310.  
  311. /*GLUT MODE */
  312. glutInit(&argc,argv);
  313. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  314. glutInitWindowSize(SCREEN_W,SCREEN_H);
  315. glutInitWindowPosition(100,100);
  316. glutCreateWindow("SOALNIIII");
  317. glutMouseFunc(mouse);
  318. gutDisplayFunc(display);
  319.  
  320. glutIdleFunc(idle);
  321.  
  322. myInit();
  323. glutMainLoop();
  324. return 0;
  325. }
  326.  
Last edited by Ancient Dragon; Dec 5th, 2008 at 9:41 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 329
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: file polygon.h

 
1
  #2
Dec 5th, 2008
Ask the author..
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: file polygon.h

 
1
  #3
Dec 5th, 2008
...and familiarize yourself with code tags. That will increase your chance to get an answer enormously.
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC