Tiny graphics library for STM32F746G-DISCO board

Dependents:   RadarDemo 3DDemo RadarDemoT

Committer:
karpent
Date:
Fri Nov 11 12:05:16 2016 +0000
Revision:
3:1ddc4aa1e5cb
Parent:
0:566855d63a2f
Missing method DrawChar() added, DrawText() corrected.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
karpent 0:566855d63a2f 1 //
karpent 0:566855d63a2f 2 // GeometricPrim.cpp - Geometric primitives class
karpent 0:566855d63a2f 3 //
karpent 0:566855d63a2f 4
karpent 0:566855d63a2f 5 #include "stdlib.h" // for abs
karpent 0:566855d63a2f 6 #include "math.h" // for floor
karpent 0:566855d63a2f 7 #include "GeometricPrim.h"
karpent 0:566855d63a2f 8
karpent 0:566855d63a2f 9
karpent 0:566855d63a2f 10 GeometricPrim::GeometricPrim()
karpent 0:566855d63a2f 11 {
karpent 0:566855d63a2f 12 }
karpent 0:566855d63a2f 13
karpent 0:566855d63a2f 14
karpent 0:566855d63a2f 15 GeometricPrim::~GeometricPrim()
karpent 0:566855d63a2f 16 {
karpent 0:566855d63a2f 17 }
karpent 0:566855d63a2f 18
karpent 0:566855d63a2f 19
karpent 0:566855d63a2f 20 void GeometricPrim::DrawLine(int startX, int startY, int endX, int endY)
karpent 0:566855d63a2f 21 {
karpent 0:566855d63a2f 22 int diffX = (endX - startX);
karpent 0:566855d63a2f 23 int diffY = (endY - startY);
karpent 0:566855d63a2f 24
karpent 0:566855d63a2f 25 uint32_t _colorMask = GetDrawColor();
karpent 0:566855d63a2f 26
karpent 0:566855d63a2f 27 if (abs(diffX) > abs(diffY)) {
karpent 0:566855d63a2f 28
karpent 0:566855d63a2f 29 float dy = diffY / (float)diffX;
karpent 0:566855d63a2f 30
karpent 0:566855d63a2f 31 if (endX > startX) {
karpent 0:566855d63a2f 32 for (int x = startX; x <= endX; x++) {
karpent 0:566855d63a2f 33 this->DrawPoint(x, (int)floor(startY + dy*(x - startX)), _colorMask);
karpent 0:566855d63a2f 34 }
karpent 0:566855d63a2f 35 }
karpent 0:566855d63a2f 36 else {
karpent 0:566855d63a2f 37 for (int x = startX; x >= endX; x--) {
karpent 0:566855d63a2f 38 this->DrawPoint(x, (int)floor(startY + dy*(x - startX)), _colorMask);
karpent 0:566855d63a2f 39 }
karpent 0:566855d63a2f 40 }
karpent 0:566855d63a2f 41 }
karpent 0:566855d63a2f 42
karpent 0:566855d63a2f 43 else {
karpent 0:566855d63a2f 44 float dx = diffX / (float)diffY;
karpent 0:566855d63a2f 45 if (endY > startY) {
karpent 0:566855d63a2f 46 for (int y = startY; y <= endY; y++) {
karpent 0:566855d63a2f 47 this->DrawPoint((int)floor(startX + dx*(y - startY)), y, _colorMask);
karpent 0:566855d63a2f 48 }
karpent 0:566855d63a2f 49 }
karpent 0:566855d63a2f 50 else {
karpent 0:566855d63a2f 51 for (int y = startY; y >= endY; y--) {
karpent 0:566855d63a2f 52 this->DrawPoint((int)floor(startX + dx*(y - startY)), y, _colorMask);
karpent 0:566855d63a2f 53 }
karpent 0:566855d63a2f 54 }
karpent 0:566855d63a2f 55 }
karpent 0:566855d63a2f 56 }
karpent 0:566855d63a2f 57
karpent 0:566855d63a2f 58
karpent 0:566855d63a2f 59 void GeometricPrim::DrawCircle(int posX, int posY, uint16_t radius)
karpent 0:566855d63a2f 60 {
karpent 0:566855d63a2f 61 int decision; /* Decision Variable */
karpent 0:566855d63a2f 62 uint32_t current_x; /* Current X Value */
karpent 0:566855d63a2f 63 uint32_t current_y; /* Current Y Value */
karpent 0:566855d63a2f 64
karpent 0:566855d63a2f 65 decision = 3 - (radius << 1);
karpent 0:566855d63a2f 66 current_x = 0;
karpent 0:566855d63a2f 67 current_y = radius;
karpent 0:566855d63a2f 68
karpent 0:566855d63a2f 69 uint32_t _colorMask=GetDrawColor();
karpent 0:566855d63a2f 70
karpent 0:566855d63a2f 71 while (current_x <= current_y)
karpent 0:566855d63a2f 72 {
karpent 0:566855d63a2f 73 DrawPoint((posX + current_x), (posY - current_y), _colorMask);
karpent 0:566855d63a2f 74
karpent 0:566855d63a2f 75 DrawPoint((posX - current_x), (posY - current_y), _colorMask);
karpent 0:566855d63a2f 76
karpent 0:566855d63a2f 77 DrawPoint((posX + current_y), (posY - current_x), _colorMask);
karpent 0:566855d63a2f 78
karpent 0:566855d63a2f 79 DrawPoint((posX - current_y), (posY - current_x), _colorMask);
karpent 0:566855d63a2f 80
karpent 0:566855d63a2f 81 DrawPoint((posX + current_x), (posY + current_y), _colorMask);
karpent 0:566855d63a2f 82
karpent 0:566855d63a2f 83 DrawPoint((posX - current_x), (posY + current_y), _colorMask);
karpent 0:566855d63a2f 84
karpent 0:566855d63a2f 85 DrawPoint((posX + current_y), (posY + current_x), _colorMask);
karpent 0:566855d63a2f 86
karpent 0:566855d63a2f 87 DrawPoint((posX - current_y), (posY + current_x), _colorMask);
karpent 0:566855d63a2f 88
karpent 0:566855d63a2f 89 if (decision < 0)
karpent 0:566855d63a2f 90 {
karpent 0:566855d63a2f 91 decision += (current_x << 2) + 6;
karpent 0:566855d63a2f 92 }
karpent 0:566855d63a2f 93 else
karpent 0:566855d63a2f 94 {
karpent 0:566855d63a2f 95 decision += ((current_x - current_y) << 2) + 10;
karpent 0:566855d63a2f 96 current_y--;
karpent 0:566855d63a2f 97 }
karpent 0:566855d63a2f 98 current_x++;
karpent 0:566855d63a2f 99 }
karpent 0:566855d63a2f 100 }
karpent 0:566855d63a2f 101
karpent 0:566855d63a2f 102
karpent 0:566855d63a2f 103 void GeometricPrim::DrawRectangle(int startX, int startY, int endX, int endY)
karpent 0:566855d63a2f 104 {
karpent 0:566855d63a2f 105 DrawLine(startX, startY, startX, endY);
karpent 0:566855d63a2f 106 DrawLine(startX, endY, endX, endY);
karpent 0:566855d63a2f 107 DrawLine(endX, endY, endX, startY);
karpent 0:566855d63a2f 108 DrawLine(endX, startY, startX, startY);
karpent 0:566855d63a2f 109 }
karpent 0:566855d63a2f 110
karpent 0:566855d63a2f 111
karpent 0:566855d63a2f 112 void GeometricPrim::DrawTriangle(int x1, int y1, int x2, int y2, int x3, int y3)
karpent 0:566855d63a2f 113 {
karpent 0:566855d63a2f 114 DrawLine(x1, y1, x2, y2);
karpent 0:566855d63a2f 115 DrawLine(x2, y2, x3, y3);
karpent 0:566855d63a2f 116 DrawLine(x3, y3, x1, y1);
karpent 0:566855d63a2f 117 }