Miroslaw K. / Graphics

Dependents:   RadarDemo 3DDemo RadarDemoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GeometricPrim.cpp Source File

GeometricPrim.cpp

00001 //
00002 // GeometricPrim.cpp - Geometric primitives class
00003 //
00004 
00005 #include "stdlib.h" // for abs
00006 #include "math.h"   // for floor
00007 #include "GeometricPrim.h"
00008 
00009 
00010 GeometricPrim::GeometricPrim()
00011 {
00012 }
00013 
00014 
00015 GeometricPrim::~GeometricPrim()
00016 {
00017 }
00018 
00019 
00020 void GeometricPrim::DrawLine(int startX, int startY, int endX, int endY)
00021 {
00022     int diffX = (endX - startX);
00023     int diffY = (endY - startY);
00024     
00025     uint32_t _colorMask = GetDrawColor();
00026 
00027     if (abs(diffX) > abs(diffY)) {
00028 
00029         float dy = diffY / (float)diffX;
00030 
00031         if (endX > startX) {
00032             for (int x = startX; x <= endX; x++) {
00033                 this->DrawPoint(x, (int)floor(startY + dy*(x - startX)), _colorMask);
00034             }
00035         }
00036         else {
00037             for (int x = startX; x >= endX; x--) {
00038                 this->DrawPoint(x, (int)floor(startY + dy*(x - startX)), _colorMask);
00039             }
00040         }
00041     }
00042 
00043     else {
00044         float dx = diffX / (float)diffY;
00045         if (endY > startY) {
00046             for (int y = startY; y <= endY; y++) {
00047                 this->DrawPoint((int)floor(startX + dx*(y - startY)), y, _colorMask);
00048             }
00049         }
00050         else {
00051             for (int y = startY; y >= endY; y--) {
00052                 this->DrawPoint((int)floor(startX + dx*(y - startY)), y, _colorMask);
00053             }
00054         }
00055     }
00056 }
00057 
00058 
00059 void GeometricPrim::DrawCircle(int posX, int posY, uint16_t radius)
00060 {
00061     int   decision;    /* Decision Variable */
00062     uint32_t  current_x;   /* Current X Value */
00063     uint32_t  current_y;   /* Current Y Value */
00064 
00065     decision = 3 - (radius << 1);
00066     current_x = 0;
00067     current_y = radius;
00068     
00069     uint32_t _colorMask=GetDrawColor();
00070 
00071     while (current_x <= current_y)
00072     {
00073         DrawPoint((posX + current_x), (posY - current_y), _colorMask);
00074 
00075         DrawPoint((posX - current_x), (posY - current_y), _colorMask);
00076 
00077         DrawPoint((posX + current_y), (posY - current_x), _colorMask);
00078 
00079         DrawPoint((posX - current_y), (posY - current_x), _colorMask);
00080 
00081         DrawPoint((posX + current_x), (posY + current_y), _colorMask);
00082 
00083         DrawPoint((posX - current_x), (posY + current_y), _colorMask);
00084 
00085         DrawPoint((posX + current_y), (posY + current_x), _colorMask);
00086 
00087         DrawPoint((posX - current_y), (posY + current_x), _colorMask);
00088 
00089         if (decision < 0)
00090         {
00091             decision += (current_x << 2) + 6;
00092         }
00093         else
00094         {
00095             decision += ((current_x - current_y) << 2) + 10;
00096             current_y--;
00097         }
00098         current_x++;
00099     }
00100 }
00101 
00102 
00103 void GeometricPrim::DrawRectangle(int startX, int startY, int endX, int endY)
00104 {
00105     DrawLine(startX, startY, startX, endY);
00106     DrawLine(startX, endY, endX, endY);
00107     DrawLine(endX, endY, endX, startY);
00108     DrawLine(endX, startY, startX, startY);
00109 }
00110 
00111 
00112 void GeometricPrim::DrawTriangle(int x1, int y1, int x2, int y2, int x3, int y3)
00113 {
00114     DrawLine(x1, y1, x2, y2);
00115     DrawLine(x2, y2, x3, y3);
00116     DrawLine(x3, y3, x1, y1);
00117 }