Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Paste Description for C++ - Umsetzung fr Linien und Kr

Umsetzung des bresenham'schen Linienalgorithmus und der Kreisvariante in C++

C++ - Umsetzung fr Linien und Kr
Thursday, August 28th, 2008 at 10:30:59am MDT 

  1. #include <iostream>
  2.  
  3. #ifndef _GLUT_
  4. #include <GL/glut.h>
  5. #define _GLUT_
  6. #endif
  7.  
  8. #include "basic_algos.h"
  9.  
  10. using namespace std;
  11.  
  12. void basic_algos::drawPoint(int x, int y)
  13. {
  14.         glVertex2i(x, y);
  15. }
  16.  
  17. void basic_algos::drawLine(int x1, int y1, int x2, int y2)
  18. {
  19.         // Schritt in x- und Y-Richtung positiv initialisieren
  20.         int incX = 1;
  21.         int incY = 1;
  22.  
  23.         // Delta von X und Y ausrechnen
  24.         int dx = x2-x1;
  25.         int dy = y2-y1;
  26.  
  27.         // Wenn die Linie in eine negative Richtung läuft, die Schrittvariablen negieren
  28.         // und das Delta der jeweiligen Richtung negieren
  29.         if(x1>x2)
  30.         {
  31.                 incX = -1;
  32.                 dx *= -1;
  33.         }
  34.         if(y1>y2)
  35.         {
  36.                 incY = -1;
  37.                 dy *= -1;
  38.         }
  39.  
  40.         // Zähler für schnelle und langsame Richtung initialisieren
  41.         int incFast = incX;
  42.         int incSlow = incY;
  43.  
  44.         // Variablen für die aktuelle X- und Y-Position
  45.         int currentX = x1;
  46.         int currentY = y1;
  47.  
  48.         // Delta für schnelle Richtung und langsame Richtung initialisieren
  49.         int dFast = dx;
  50.         int dSlow = dy;
  51.  
  52.         // Zeiger auf aktuelle Position in schnelle und in langsame Richtung setzen
  53.         int *currentFast = &currentX;;
  54.         int *currentSlow = &currentY;
  55.  
  56.         // Wenn es sich um eine schnell steigende Linie handelt...
  57.         if(dy > dx)
  58.         {
  59.                 // ... dann Delta für schnelle Richtung auf den Wert von Delta-Y setzen und
  60.                 // Delta für langsame Richtung auf den Wert von Delta-X setzen...
  61.                 dFast = dy;
  62.                 dSlow = dx;
  63.  
  64.                 // ... dann Zähler für schnelle Richtung auf den Zähler von Y-Richtung setzen und
  65.                 // Zähler für langsame Richtung auf den Zähler von X-Richtung setzen ...
  66.                 incFast = incY;
  67.                 incSlow = incX;
  68.  
  69.                 // ... sowie Zeiger für schnelle Position auf Variable der Y-Position setzen und
  70.                 // Zeiger für langsame Position auf Variable der X-Position setzen.
  71.                 currentFast = &currentY;
  72.                 currentSlow = &currentX;
  73.         }
  74.  
  75.         // Fehlervariable mit der hälfte des schnellen Deltas initialisieren (immer abgerundet)
  76.         int error = (int)dFast / 2;
  77.  
  78.         drawPoint(currentX, currentY);
  79.  
  80.         // Solange die aktuelle X/Y-Position nicht mit den Koordinaten des Endpunktes übereinstimmt...
  81.         while(currentX != x2 || currentY != y2)
  82.         {
  83.                 // ... einen Schritt in die schnelle Position machen
  84.                 *currentFast += incFast;
  85.                 // ... Delta der langsamen Richtung von der Fehlervariable abziehen
  86.                 error -= dSlow;
  87.  
  88.                 // Wenn der Fehlerwert unter 0 fällt ...
  89.                 if(error < 0)
  90.                 {
  91.                         // ... dann das Delta der schnellen Richtung aufaddieren ...
  92.                         error += dFast;
  93.                         // ... und einen Schritt in die langsame Richtung machen
  94.                         *currentSlow += incSlow;
  95.                 }
  96.  
  97.         drawPoint(currentX, currentY);
  98.         }
  99. }
  100.  
  101. void basic_algos::drawCircle(int x, int y, int r)
  102. {
  103.         // Positionen an denen aktuell gezeichnet werden soll
  104.         int currentX = r;
  105.         int currentY = 0;
  106.  
  107.         // Fehlervariable
  108.         int error = r;
  109.  
  110.         // Deltas (nicht wirklich aber zum Verständnis)
  111.         int dx;
  112.         int dy;
  113.  
  114.         // Zeichnen aller vier Startpunkte
  115.         drawPoint(x+currentX, y+currentY);
  116.         drawPoint(x-currentX, y+currentY);
  117.        
  118.         drawPoint(x+currentY, y+currentX);
  119.         drawPoint(x+currentY, y-currentX);
  120.  
  121.         // Solange Pixel zeichnen, bis Oktand fertig berechnet wurde
  122.         while(currentY < currentX)
  123.         {
  124.                 // dy berechnen
  125.                 dy = currentY * 2 + 1;
  126.                 // Einen Schritt in Y-Richtung machen
  127.                 currentY += 1;
  128.                 // Fehlerwert verringern
  129.                 error -= dy;
  130.  
  131.                 // Wenn der Fehler unter 0 sinkt
  132.                 if(error < 0)
  133.                 {
  134.                         // dx berechnen
  135.                         dx = 1 - currentX * 2;
  136.                         // einen Schritt in X-Richtung machen
  137.                         currentX -= 1;
  138.                         // Fehler wieder über 0 bringen
  139.                         error -= dx;
  140.                 }
  141.  
  142.                 // berechneten Punkt in allen acht Oktanden zeichnen
  143.                 drawPoint(x+currentX, y+currentY);
  144.                 drawPoint(x-currentX, y+currentY);
  145.                 drawPoint(x-currentX, y-currentY);
  146.                 drawPoint(x+currentX, y-currentY);
  147.                
  148.                 drawPoint(x+currentY, y+currentX);
  149.                 drawPoint(x-currentY, y+currentX);
  150.                 drawPoint(x-currentY, y-currentX);
  151.                 drawPoint(x+currentY, y-currentX);
  152.         }
  153. }

Paste Details

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right
fantasy-obligation
fantasy-obligation