/pr2/UFP.cpp

http://igr0809.googlecode.com/
C++ | 858 lines | 637 code | 127 blank | 94 comment | 133 complexity | e435ed1d2a04cbe8372dc853e590d8c2 MD5 | raw file
  1. /* Práctica 1 de IGr
  2. *
  3. * Enrique López Ma?as - 70060945-R
  4. * Daniel Dionne González - 51454901-Z
  5. */
  6. //---------------------------------------------------------------------------
  7. #include <vcl.h>
  8. #pragma hdrstop
  9. #include "UFP.h"
  10. //---------------------------------------------------------------------------
  11. #pragma package(smart_init)
  12. #pragma resource "*.dfm"
  13. TGLForm2D *GLForm2D;
  14. //---------------------------------------------------------------------------
  15. __fastcall TGLForm2D::TGLForm2D(TComponent* Owner)
  16. : TForm(Owner)
  17. {
  18. }
  19. //---------------------------------------------------------------------------
  20. void __fastcall TGLForm2D::FormCreate(TObject *Sender)
  21. {
  22. scene = new Scene();
  23. running = true;
  24. RatioViewPort = 1.0;
  25. hdc = GetDC(Handle);
  26. SetPixelFormatDescriptor();
  27. hrc = wglCreateContext(hdc);
  28. if(hrc == NULL)
  29. ShowMessage(":-)~ hrc == NULL");
  30. if(wglMakeCurrent(hdc, hrc) == false)
  31. ShowMessage("Could not MakeCurrent");
  32. //Color de fondo de la ventana
  33. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  34. // Inicializacion del volumen de vista
  35. scene->setLimits(-300,300,300,-300);
  36. //Radio del volumen de vista == 1
  37. // Inicializacion del puerto de vista
  38. //ClientWidth=400;
  39. //ClientHeight=400;
  40. // Inicializacion de las variables del programa
  41. traslX = traslY = 0;
  42. formDebug = new TFormDebug(this);
  43. formPaleta = new TformPaleta(this);
  44. zoomVel = 0;
  45. maxZoomVel = 200;
  46. xVel = 0;
  47. yVel = 0;
  48. maxVel = 200;
  49. pointInit.setX(0);
  50. pointInit.setY(0);
  51. option = unSelected;
  52. formPaleta->editLados->Text = "5";
  53. }
  54. //---------------------------------------------------------------------------
  55. void __fastcall TGLForm2D::SetPixelFormatDescriptor()
  56. {
  57. PIXELFORMATDESCRIPTOR pfd = {
  58. sizeof(PIXELFORMATDESCRIPTOR),
  59. 1,
  60. PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
  61. PFD_TYPE_RGBA,
  62. 32,
  63. 0,0,0,0,0,0,
  64. 0,0,
  65. 0,0,0,0,0,
  66. 32,
  67. 0,
  68. 0,
  69. PFD_MAIN_PLANE,
  70. 0,
  71. 0,0,0
  72. };
  73. int PixelFormat = ChoosePixelFormat(hdc, &pfd);
  74. SetPixelFormat(hdc, PixelFormat, &pfd);
  75. }
  76. //---------------------------------------------------------------------
  77. void __fastcall TGLForm2D::FormResize(TObject *Sender)
  78. {
  79. if (scene!=NULL) {
  80. //se actualiza puerto de vista y su radio
  81. if ((ClientWidth<=1)||(ClientHeight<=1)){
  82. ClientWidth=400;
  83. ClientHeight=400;
  84. RatioViewPort=1.0;
  85. }
  86. else RatioViewPort= (float)ClientWidth/(float)ClientHeight;
  87. glViewport(0,0,ClientWidth,ClientHeight);
  88. // se actualiza el volumen de vista
  89. // para que su radio coincida con ratioViewPort
  90. if (scene->getYTop() == 0) scene->setYTop(1);
  91. GLfloat RatioVolVista=scene->getXRight()/scene->getYTop();
  92. if (RatioVolVista>=RatioViewPort) {
  93. //Aumentamos yTop-yBot
  94. scene->setYTop(scene->getXRight()/RatioViewPort);
  95. scene->setYBot(-scene->getYTop());
  96. } else {
  97. //Aumentamos xRight-xLeft
  98. scene->setXRight(RatioViewPort*scene->getYTop());
  99. scene->setXLeft(-scene->getXRight());
  100. }
  101. glMatrixMode(GL_PROJECTION);
  102. glLoadIdentity();
  103. gluOrtho2D(scene->getXLeft() + traslX,
  104. scene->getXRight() + traslX,
  105. scene->getYBot() + traslY,
  106. scene->getYTop() + traslY);
  107. glMatrixMode(GL_MODELVIEW);
  108. glLoadIdentity();
  109. GLScene();
  110. }
  111. }
  112. //---------------------------------------------------------------------------
  113. void __fastcall TGLForm2D::GLScene()
  114. {
  115. if (running) {
  116. if (divisions<1) divisions=1;
  117. glClear(GL_COLOR_BUFFER_BIT);
  118. // draw scene
  119. for (int i=0; i<scene->getNumberOfLines(); i++) {
  120. LineDrawing *ld = scene->getLineDrawing(i);
  121. glBegin(GL_LINES);
  122. ld->savePosition();
  123. ld->iniciar();
  124. glColor3f(1.0f, 1.0f, 1.0f);
  125. for (int j=0; j<ld->getNumberOfPoints(); j++) {
  126. if (ld==selectedld) {
  127. glColor3f(1.0f, 0.0f, 0.0f);
  128. }
  129. glVertex3f(ld->getPoint1().getX(), ld->getPoint1().getY(), 1.0f);
  130. glVertex3f(ld->getPoint2().getX(), ld->getPoint2().getY(), 1.0f);
  131. ld->avanzar();
  132. }
  133. glEnd();
  134. ld->iniciar();
  135. glPointSize(3.0f);
  136. glBegin(GL_POINTS);
  137. glVertex2f(ld->getPoint1().getX(), ld->getPoint1().getY());
  138. glEnd();
  139. for (int j=0; j<ld->getNumberOfPoints(); j++) {
  140. if ((ld==hoveredld ||ld==selectedld) && option == seleccionar) {
  141. glPointSize(5.0f);
  142. glColor3f(1.0f, 0.0f, 0.0f);
  143. }
  144. else
  145. glColor3f(0.0f, 0.0f, 1.0f);
  146. glBegin(GL_POINTS);
  147. glPointSize(3.0f);
  148. glVertex2f(ld->getPoint2().getX(), ld->getPoint2().getY());
  149. ld->avanzar();
  150. glEnd();
  151. }
  152. ld->restorePosition();
  153. }
  154. // si estoy pintando el rectángulo del algoritmo de recorte, pintarlo
  155. if (option == recortar2) {
  156. glColor3f(0.0f, 1.0f, 1.0f);
  157. glBegin(GL_LINES);
  158. glVertex2f(pointInit.getX(), pointInit.getY());
  159. glVertex2f(pointMouse.getX(), pointInit.getY());
  160. glVertex2f(pointMouse.getX(), pointInit.getY());
  161. glVertex2f(pointMouse.getX(), pointMouse.getY());
  162. glVertex2f(pointMouse.getX(), pointMouse.getY());
  163. glVertex2f(pointInit.getX(), pointMouse.getY());
  164. glVertex2f(pointInit.getX(), pointMouse.getY());
  165. glVertex2f(pointInit.getX(), pointInit.getY());
  166. glEnd();
  167. }
  168. glFlush();
  169. SwapBuffers(hdc);
  170. }
  171. }
  172. //---------------------------------------------------------------------------
  173. void __fastcall TGLForm2D::FormPaint(TObject *Sender)
  174. {
  175. GLScene();
  176. }
  177. //---------------------------------------------------------------------------
  178. void TGLForm2D::drawSceneCenter() {
  179. glPointSize(3.0f);
  180. glBegin(GL_POINTS);
  181. glColor3f(1.0f, 1.0f, 0);
  182. glVertex2i(traslX,traslY);
  183. glEnd();
  184. }
  185. void __fastcall TGLForm2D::FormDestroy(TObject *Sender)
  186. {
  187. ReleaseDC(Handle,hdc);
  188. wglMakeCurrent(NULL, NULL);
  189. wglDeleteContext(hrc);
  190. }
  191. //---------------------------------------------------------------------------
  192. void __fastcall TGLForm2D::FormKeyDown(TObject *Sender, WORD &Key,
  193. TShiftState Shift)
  194. {
  195. key = Key;
  196. //Key up
  197. if ( key==37 ) {
  198. traslX++;
  199. } else if ( key==38 ) {
  200. traslY--;
  201. } else if ( key==39 ) {
  202. traslX--;
  203. } else if ( key==40 ) {
  204. traslY++;
  205. } else if ( key==46 ){
  206. scene->elimina(selectedld);
  207. }
  208. Resize();
  209. }
  210. //---------------------------------------------------------------------------
  211. void __fastcall TGLForm2D::Dep1Click(TObject *Sender)
  212. {
  213. formDebug->Visible = !formDebug->Visible;
  214. }
  215. //---------------------------------------------------------------------------
  216. void __fastcall TGLForm2D::RedrawTimerTimer(TObject *Sender)
  217. {
  218. //if (running) {
  219. // Key -
  220. if (key==109) {
  221. if (zoomVel<maxZoomVel) zoomVel++;
  222. }
  223. // Key +
  224. else if (key==107) {
  225. if (zoomVel>-maxZoomVel) zoomVel--;
  226. }
  227. // Key up
  228. else if (key==38) {
  229. if (maxVel>yVel) yVel++;
  230. }
  231. // Key down
  232. else if (key==40) {
  233. if (maxVel>-yVel) yVel--;
  234. }
  235. // Key left
  236. else if (key==37) {
  237. if (maxVel>-xVel) xVel--;
  238. }
  239. // Key right
  240. else if (key==39) {
  241. if (maxVel>xVel) xVel++;
  242. }
  243. if (scene!=NULL) {
  244. // Modificar límites de la parte visible de la escena
  245. // Límite para que la imagen no se invierta
  246. // (xRight debe ser siempre positivo, y mejor
  247. // si no llega 0, porque pasan cosas raras)
  248. float xRight = scene->getXRight();
  249. if (xRight+zoomVel>10) {
  250. traslY+=yVel;
  251. traslX+=xVel;
  252. float ratio = scene->getRatio();
  253. xRight = xRight+zoomVel;
  254. scene->setXRight(xRight);
  255. scene->setXLeft(-xRight);
  256. scene->setYTop(xRight/ratio);
  257. scene->setYBot(-scene->getYTop());
  258. // Modificar matriz de proyeccion
  259. glMatrixMode(GL_PROJECTION);
  260. glLoadIdentity();
  261. gluOrtho2D(scene->getXLeft() + traslX,
  262. scene->getXRight() + traslX,
  263. scene->getYBot() + traslY,
  264. scene->getYTop() + traslY);
  265. // Dibujar
  266. GLScene();
  267. } }
  268. // }
  269. }
  270. //---------------------------------------------------------------------------
  271. void __fastcall TGLForm2D::FormKeyUp(TObject *Sender, WORD &Key,
  272. TShiftState Shift)
  273. {
  274. key = 0;
  275. }
  276. //---------------------------------------------------------------------------
  277. void __fastcall TGLForm2D::BrakeTimerTimer(TObject *Sender)
  278. {
  279. if (key!=107 && key!=109) {
  280. // Reset zoomVel
  281. if (zoomVel>0) zoomVel--;
  282. else if (zoomVel<0) zoomVel++;
  283. }
  284. if (key!=38 && key!=40) {
  285. // Reset yVel
  286. if (yVel>0) yVel--;
  287. else if (yVel<0) yVel++;
  288. }
  289. if (key!=37 && key!=49) {
  290. // Reset xVel
  291. if (xVel>0) xVel--;
  292. else if (xVel<0) xVel++;
  293. }
  294. }
  295. //---------------------------------------------------------------------------
  296. void __fastcall TGLForm2D::Paleta1Click(TObject *Sender)
  297. {
  298. formPaleta->Visible = !formPaleta->Visible; Resize();
  299. }
  300. //---------------------------------------------------------------------------
  301. void __fastcall TGLForm2D::FormMouseDown(TObject *Sender,
  302. TMouseButton Button, TShiftState Shift, int X, int Y)
  303. {
  304. // Get clicked point
  305. float width = scene->getXRight() - scene->getXLeft();
  306. float ratio = width/ClientWidth;
  307. float x = (X - ClientWidth/2) * ratio + traslX; // horizontal
  308. float y = (ClientHeight/2 - Y) * ratio + traslY; // vertical
  309. switch (option) {
  310. case (spiral): {
  311. drawSpiral(50,0,10,1,5,*new GLPoint(x,y));
  312. break;
  313. }
  314. case (polygon): {
  315. drawPolygon(StrToInt(formPaleta->editLados->Text),100,*new GLPoint(x,y));
  316. break;
  317. }
  318. case (startPolilines): {
  319. scene->startNewDrawing(*new GLPoint(x,y));
  320. option = polilines;
  321. break;
  322. }
  323. case (polilines): {
  324. Pen p = *new Pen(scene);
  325. p.moveTo(x,y,true);
  326. break;
  327. }
  328. case (seleccionar) : {
  329. selectedld = scene->getLineDrawing(*new GLPoint(x,y), 5*ratio);
  330. break;
  331. }
  332. case (recortar1) :
  333. {
  334. pointInit = *new GLPoint(pointMouse.getX(),pointMouse.getY());
  335. option = recortar2;
  336. break;
  337. }
  338. case (recortar2) : {
  339. for (int i=0; i<scene->getNumberOfLines(); i++) {
  340. LineDrawing *ld = scene->getLineDrawing(i);
  341. glBegin(GL_LINES);
  342. ld->iniciar();
  343. glColor3f(1.0f, 1.0f, 1.0f);
  344. for (int j=0; j<ld->getNumberOfPoints(); j++) {
  345. if (scene->getNumberOfLines()>0) {
  346. GLPoint a = *new GLPoint(ld->getPoint1().getX(), ld->getPoint1().getY());
  347. GLPoint b = *new GLPoint(ld->getPoint2().getX(), ld->getPoint2().getY());
  348. if (recortar(a,b)) {
  349. ld->setPoint1(a);
  350. ld->setPoint2(b);
  351. ld->avanzar();
  352. } else {
  353. ld->deleteSegment();
  354. j--;
  355. // Si el dibujo queda vacio, borrarlo
  356. if (ld->getNumberOfPoints()==0)
  357. scene->deleteLineDrawing();
  358. }
  359. }
  360. }
  361. }
  362. option = unSelected;
  363. break;
  364. }
  365. case (koch1) :
  366. {
  367. pointInit = *new GLPoint(pointMouse.getX(),pointMouse.getY());
  368. option = recortar2;
  369. break;
  370. }
  371. case (koch2) :
  372. {
  373. pointInit = *new GLPoint(pointMouse.getX(),pointMouse.getY());
  374. option = recortar2;
  375. break;
  376. }
  377. case (dragon) :
  378. {
  379. pointInit = *new GLPoint(pointMouse.getX(),pointMouse.getY());
  380. option = recortar2;
  381. break;
  382. }
  383. }
  384. }
  385. //---------------------------------------------------------------------------
  386. void __fastcall TGLForm2D::FormMouseMove(TObject *Sender,
  387. TShiftState Shift, int X, int Y)
  388. {
  389. float width = scene->getXRight() - scene->getXLeft();
  390. float ratio = width/ClientWidth;
  391. float x = (X - ClientWidth/2) * ratio + traslX; // horizontal
  392. float y = (ClientHeight/2 - Y) * ratio + traslY; // vertical
  393. pointMouse = *new GLPoint(x,y);
  394. hoveredld = scene->getLineDrawing(*new GLPoint(x,y), 5*ratio);
  395. }
  396. //---------------------------------------------------------------------------
  397. void TGLForm2D::drawPolygon(int sides, float radius, GLPoint center) {
  398. Pen *p = new Pen(scene);
  399. // Start to the right and turn left
  400. GLPoint origin = *new GLPoint(center.getX() + radius, center.getY());
  401. p->moveTo(origin, false);
  402. float alpha = 2*M_PI/sides;
  403. float beta = (M_PI-alpha);
  404. float side = 2*cos(beta/2)*radius;
  405. p->turn(M_PI-beta/2);
  406. scene->startNewDrawing(p->getPosition());
  407. for (int i=0; i<sides-1; i++) {
  408. p->move(side, true);
  409. p->turn(M_PI-beta);
  410. }
  411. p->moveTo(origin, true);
  412. delete p;
  413. };
  414. void TGLForm2D::drawSpiral(int sides, float direction, float side, float dir_inc, float side_inc, GLPoint start) {
  415. Pen *p = new Pen(scene, start, direction);
  416. scene->startNewDrawing(start);
  417. for (int i=0; i<sides; i++) {
  418. p->move(side, true);
  419. p->turn(dir_inc);
  420. side += side_inc;
  421. }
  422. delete p;
  423. };
  424. void __fastcall TGLForm2D::Lneas1Click(TObject *Sender)
  425. {
  426. option = startPolilines;
  427. }
  428. //---------------------------------------------------------------------------
  429. void __fastcall TGLForm2D::Polgono1Click(TObject *Sender)
  430. {
  431. option = polygon;
  432. }
  433. //---------------------------------------------------------------------------
  434. void __fastcall TGLForm2D::Espiral1Click(TObject *Sender)
  435. {
  436. option = spiral;
  437. }
  438. //---------------------------------------------------------------------------
  439. void __fastcall TGLForm2D::zoomMasClick(TObject *Sender)
  440. {
  441. if (zoomVel>-maxZoomVel) zoomVel-=5;
  442. }
  443. //---------------------------------------------------------------------------
  444. void __fastcall TGLForm2D::zoomMinusClick(TObject *Sender)
  445. {
  446. if (zoomVel<maxZoomVel) zoomVel+=5;
  447. }
  448. //---------------------------------------------------------------------------
  449. void __fastcall TGLForm2D::Button1Click(TObject *Sender)
  450. {
  451. if (maxVel>yVel) yVel+=5;
  452. }
  453. //---------------------------------------------------------------------------
  454. void __fastcall TGLForm2D::Button3Click(TObject *Sender)
  455. {
  456. if (maxVel>-yVel) yVel-=5;
  457. }
  458. //---------------------------------------------------------------------------
  459. void __fastcall TGLForm2D::Button2Click(TObject *Sender)
  460. {
  461. if (maxVel>-xVel) xVel-=5;
  462. }
  463. //---------------------------------------------------------------------------
  464. void __fastcall TGLForm2D::Button4Click(TObject *Sender)
  465. {
  466. if (maxVel>-xVel) xVel+=5;
  467. }
  468. //---------------------------------------------------------------------------
  469. void __fastcall TGLForm2D::Nuevo1Click(TObject *Sender)
  470. {
  471. running = false;
  472. delete scene;
  473. scene = new Scene();
  474. scene->setLimits(-300,300,300,-300);
  475. RatioViewPort = 1.0;
  476. SetPixelFormatDescriptor();
  477. traslX = traslY = 0;
  478. zoomVel = 0;
  479. maxZoomVel = 200;
  480. xVel = 0;
  481. yVel = 0;
  482. pointInit.setX(0);
  483. pointInit.setY(0);
  484. option = unSelected;
  485. Resize();
  486. running = true;
  487. }
  488. //---------------------------------------------------------------------------
  489. void __fastcall TGLForm2D::Cortar1Click(TObject *Sender)
  490. {
  491. option = recortar1;
  492. }
  493. //---------------------------------------------------------------------------
  494. void __fastcall TGLForm2D::Seleccionar1Click(TObject *Sender)
  495. {
  496. this->option = seleccionar;
  497. }
  498. //---------------------------------------------------------------------------
  499. void __fastcall TGLForm2D::Borrar1Click(TObject *Sender)
  500. {
  501. scene->elimina(selectedld);
  502. GLScene();
  503. }
  504. //---------------------------------------------------------------------------
  505. int TGLForm2D::recortar(GLPoint &a, GLPoint &b) {
  506. /*
  507. Tras ejecutar la función, las coordenadas se convierten en los puntos
  508. resultantes del recorte.
  509. Si la linea está fuera del recorte, la función devuelve FALSE;
  510. x1, y1: Coordenada de inicio
  511. x2, y2: Coordenada de fin
  512. */
  513. #define CLIPLEFT 1 // Binario 0001
  514. #define CLIPRIGHT 2 // 0010
  515. #define CLIPLOWER 4 // 0100
  516. #define CLIPUPPER 8 // 1000
  517. float YMin, YMax, XMin, XMax;
  518. if (pointInit.getY() < pointMouse.getY()) {
  519. YMin = pointInit.getY();
  520. YMax = pointMouse.getY();
  521. } else {
  522. YMax = pointInit.getY();
  523. YMin = pointMouse.getY();
  524. }
  525. if (pointInit.getX() < pointMouse.getX()) {
  526. XMin = pointInit.getX();
  527. XMax = pointMouse.getX();
  528. } else {
  529. XMax = pointInit.getX();
  530. XMin = pointMouse.getX();
  531. }
  532. /*
  533. Estas variables indican la posición de cada punto (izquierda, derecha, arriba y
  534. abajo) usando los flags declarados).
  535. */
  536. float x1,y1,x2,y2;
  537. x1 = a.getX();
  538. y1 = a.getY();
  539. x2 = b.getX();
  540. y2 = b.getY();
  541. int K1=0, K2=0;
  542. float dx, dy;
  543. dx = x2-x1;
  544. dy = y2-y1;
  545. if (y1<YMin) K1 =CLIPLOWER;
  546. if (y1>YMax) K1 =CLIPUPPER;
  547. if (x1<XMin) K1|=CLIPLEFT;
  548. if (x1>XMax) K1|=CLIPRIGHT;
  549. if (y2<YMin) K2 =CLIPLOWER;
  550. if (y2>YMax) K2 =CLIPUPPER;
  551. if (x2<XMin) K2|=CLIPLEFT;
  552. if (x2>XMax) K2|=CLIPRIGHT;
  553. while (K1 || K2) {
  554. if(K1 & K2)
  555. return FALSE;
  556. if(K1) {
  557. if(K1 & CLIPLEFT) {
  558. y1+=(XMin-x1)*dy/dx;
  559. x1=XMin;
  560. }
  561. else if(K1 & CLIPRIGHT) {
  562. y1+=(XMax-x1)*dy/dx;
  563. x1=XMax;
  564. }
  565. if(K1 & CLIPLOWER) {
  566. x1+=(YMin-y1)*dx/dy;
  567. y1=YMin;
  568. }
  569. else if(K1 & CLIPUPPER) {
  570. x1+=(YMax-y1)*dx/dy;
  571. y1=YMax;
  572. }
  573. K1 = 0;
  574. if(y1<YMin) K1 =CLIPLOWER;
  575. if(y1>YMax) K1 =CLIPUPPER;
  576. if(x1<XMin) K1|=CLIPLEFT;
  577. if(x1>XMax) K1|=CLIPRIGHT;
  578. }
  579. if(K1 & K2)
  580. return FALSE;
  581. if(K2) {
  582. if(K2 & CLIPLEFT) {
  583. y2+=(XMin-x2)*dy/dx;
  584. x2=XMin;
  585. }
  586. else if(K2 & CLIPRIGHT) {
  587. y2+=(XMax-x2)*dy/dx;
  588. x2=XMax;
  589. }
  590. if(K2 & CLIPLOWER) {
  591. x2+=(YMin-y2)*dx/dy;
  592. y2=YMin;
  593. }
  594. else if(K2 & CLIPUPPER) {
  595. x2+=(YMax-y2)*dx/dy;
  596. y2=YMax;
  597. }
  598. K2 = 0;
  599. if(y2<YMin) K2 =CLIPLOWER;
  600. if(y2>YMax) K2 =CLIPUPPER;
  601. if(x2<XMin) K2|=CLIPLEFT;
  602. if(x2>XMax) K2|=CLIPRIGHT;
  603. }
  604. }
  605. a = *new GLPoint(x1,y1);
  606. b = *new GLPoint(x2,y2);
  607. return TRUE;
  608. };
  609. void TGLForm2D::applyTransformation (int algorithm) {
  610. Lista<Segment>* auxSegmentList=new Lista<Segment>();
  611. GLPoint pointAux;
  612. //Para cada uno de los miembros de la escena...
  613. for (int i=0; i<scene->getNumberOfLines(); i++) {
  614. //Guardamos en ld la escena i...
  615. LineDrawing *ld = scene->getLineDrawing(i);
  616. //Puntero al principio...
  617. ld->iniciar();
  618. //Si es la ListDrawing que queremos...
  619. if (ld==selectedld) {
  620. //Para cada uno de sus puntos...
  621. for (int j=0; j<ld->getNumberOfPoints(); j++) {
  622. //Almacenamos la información de sus puntos para trabajar con ellos
  623. GLfloat ox= ld->getPoint1().getX();
  624. GLfloat oy= ld->getPoint1().getY();
  625. GLfloat dx= ld->getPoint2().getX();
  626. GLfloat dy= ld->getPoint2().getY();
  627. //Cálculo de la longitud total del segmento
  628. GLfloat longitud= sqrt ((dx - ox)*(dx - ox)+ (dy - oy)*(dy - oy));
  629. if (longitud != 0) {
  630. switch (algorithm) {
  631. case 1:
  632. j+=Koch1(ld,ox,oy,dx,dy,longitud);
  633. break;
  634. case 2:
  635. j+=Koch2(ld,ox,oy,dx,dy,longitud);
  636. break;
  637. case 3:
  638. j+=Dragon(ld,ox,oy,dx,dy,longitud);
  639. break;
  640. }
  641. } else
  642. ld->avanzar();
  643. }
  644. }
  645. }
  646. }
  647. int TGLForm2D::Koch1(LineDrawing *ld, float ox, float oy, float dx, float dy, float longitud) {
  648. GLfloat cosAlfa= (dx-ox) / longitud;
  649. GLfloat sinAlfa = (dy-oy) / longitud;
  650. GLfloat alfaRad = acos(cosAlfa);
  651. if (dy < oy) alfaRad = - alfaRad;
  652. GLPoint auxA = *new GLPoint(longitud/(3.0)*cosAlfa+ox, longitud/(3.0)*sinAlfa+oy );
  653. GLPoint auxC = *new GLPoint(longitud*(2.0/3.0)*cosAlfa+ox, longitud*(2.0/3.0)*sinAlfa+oy );
  654. GLPoint auxD = *new GLPoint(dx,dy );
  655. ld->setPoint2( auxA );
  656. alfaRad += 2/6*M_PI; // Sumar 60?
  657. float cosAlfa2 = cos(alfaRad);
  658. float sinAlfa2 = sin(alfaRad);
  659. GLPoint auxB = *new GLPoint(longitud/3.0*cosAlfa2+auxA.getX(), longitud/3.0*sinAlfa2+auxA.getY());
  660. ld->insertaDespues( new Segment(auxA, auxB) );
  661. ld->insertaDespues( new Segment(auxB, auxC) );
  662. ld->insertaDespues( new Segment(auxC, auxD) );
  663. ld->avanzar();
  664. return 3;
  665. }
  666. int TGLForm2D::Koch2(LineDrawing *ld, float ox, float oy, float dx, float dy, float longitud) {
  667. GLfloat cosAlfa= (dx-ox) / longitud;
  668. GLfloat sinAlfa = (dy-oy) / longitud;
  669. GLfloat alfaRad = acos(cosAlfa);
  670. if (dy < oy) alfaRad = - alfaRad;
  671. GLfloat beta = 90+alfaRad*180/M_PI;
  672. GLfloat betaRad = beta*M_PI/180;
  673. GLfloat cosBeta = cos(betaRad);
  674. GLfloat sinBeta = sin(betaRad);
  675. GLPoint auxA = *new GLPoint(longitud/4.0*cosAlfa+ox, longitud/4.0*sinAlfa+oy );
  676. GLPoint auxB = *new GLPoint(longitud/4.0*cosBeta+auxA.getX(), longitud/4.0*sinBeta+auxA.getY());
  677. GLPoint auxC = *new GLPoint(longitud/4.0*cosAlfa+auxB.getX(), longitud/4.0*sinAlfa+auxB.getY());
  678. GLPoint auxD = *new GLPoint(-longitud/2.0*cosBeta+auxC.getX(), -longitud/2.0*sinBeta+auxC.getY());
  679. GLPoint auxE = *new GLPoint(longitud/4.0*cosAlfa+auxD.getX(), longitud/4.0*sinAlfa+auxD.getY());
  680. GLPoint auxF = *new GLPoint(longitud/4.0*cosBeta+auxE.getX(), longitud/4.0*sinBeta+auxE.getY());
  681. GLPoint auxG = *new GLPoint(dx,dy);
  682. ld->setPoint2( auxA );
  683. ld->insertaDespues( new Segment(auxA, auxB) );
  684. ld->insertaDespues( new Segment(auxB, auxC) );
  685. ld->insertaDespues( new Segment(auxC, auxD) );
  686. ld->insertaDespues( new Segment(auxD, auxE) );
  687. ld->insertaDespues( new Segment(auxE, auxF) );
  688. ld->insertaDespues( new Segment(auxF, auxG) );
  689. ld->avanzar();
  690. return 6;
  691. }
  692. int TGLForm2D::Dragon(LineDrawing *ld, float ox, float oy, float dx, float dy, float longitud) {
  693. GLfloat cosAlfa= (dx-ox) / longitud;
  694. GLfloat alfaRad = acos(cosAlfa);
  695. if (dy < oy) alfaRad = - alfaRad;
  696. alfaRad += M_PI/4; // Sumar 45?;
  697. float cosAlfa2 = cos(alfaRad);
  698. float sinAlfa2 = sin(alfaRad);
  699. float longLado = longitud/(2*cos(M_PI/4));
  700. GLPoint auxA = *new GLPoint(longLado*cosAlfa2+ox, longLado*sinAlfa2+oy );
  701. ld->setPoint2( auxA );
  702. GLPoint auxB = *new GLPoint(dx,dy);
  703. ld->insertaDespues( new Segment(auxA, auxB) );
  704. ld->avanzar();
  705. return 1;
  706. }
  707. //---------------------------------------------------------------------------
  708. void __fastcall TGLForm2D::FormClose(TObject *Sender, TCloseAction &Action)
  709. {
  710. running = false;
  711. delete scene;
  712. }
  713. //---------------------------------------------------------------------------
  714. void __fastcall TGLForm2D::Koch11Click(TObject *Sender)
  715. {
  716. if ( option == seleccionar )
  717. applyTransformation(1);
  718. }
  719. //---------------------------------------------------------------------------
  720. void __fastcall TGLForm2D::Koch21Click(TObject *Sender)
  721. {
  722. if ( option == seleccionar )
  723. applyTransformation(2);
  724. }
  725. //---------------------------------------------------------------------------
  726. void __fastcall TGLForm2D::Dragn1Click(TObject *Sender)
  727. {
  728. if ( option == seleccionar )
  729. applyTransformation(3);
  730. }
  731. //---------------------------------------------------------------------------