Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Graphics.cpp
00001 /* 00002 * libmbed-graphics 2D and wireframe 3D graphics library for the MBED 00003 * microcontroller platform 00004 * Copyright (C) <2009> Michael Sheldon <mike@mikeasoft.com> 00005 * Optimized and adapted for AbstractLCD interface 00006 * Copyright (C) <2010> Igor Skochinsky <skochinsky@gmail.com> 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Library General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Library General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Library General Public 00019 * License along with this library; if not, write to the 00020 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00021 * Boston, MA 02111-1307, USA. 00022 */ 00023 00024 #include "Graphics.h" 00025 00026 // swap two values 00027 #define SWAP(a, b) (((a) == (b)) || (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))) 00028 00029 Graphics::Graphics(AbstractLCD *lcd) 00030 : _lcd(lcd) { 00031 _cx3d = _lcd->width() / 2; 00032 _cy3d = _lcd->height() / 2; 00033 _cz3d = 150; 00034 } 00035 00036 void Graphics::line(int x0, int y0, int x1, int y1, int colour) { 00037 // Bresenham 00038 //printf("line(%d, %d, %d, %d, %d)\n", x0, y0, x1, y1, colour); 00039 bool steep = abs(y1 - y0) > abs(x1 - x0); 00040 int temp, deltax, deltay, error, ystep, y, x; 00041 if (steep) { 00042 temp = y0; 00043 y0 = x0; 00044 x0 = temp; 00045 temp = y1; 00046 y1 = x1; 00047 x1 = temp; 00048 } 00049 if (x0 > x1) { 00050 temp = x1; 00051 x1 = x0; 00052 x0 = temp; 00053 temp = y1; 00054 y1 = y0; 00055 y0 = temp; 00056 } 00057 deltax = x1 - x0; 00058 deltay = abs(y1 - y0); 00059 error = deltax / 2; 00060 y = y0; 00061 if (y0 < y1) { 00062 ystep = 1; 00063 } else { 00064 ystep = -1; 00065 } 00066 for (x=x0; x<=x1; x++) { 00067 if (steep) { 00068 _lcd->pixel(y, x, colour); 00069 } else { 00070 _lcd->pixel(x, y, colour); 00071 } 00072 error = error - deltay; 00073 if (error < 0) { 00074 y = y + ystep; 00075 error = error + deltax; 00076 } 00077 } 00078 } 00079 00080 void Graphics::line3d(int x0, int y0, int z0, int x1, int y1, int z1, int colour) { 00081 if (z0 + _cz3d <= 0 || z1 + _cz3d <= 0) { 00082 // Behind the camera 00083 return; 00084 } 00085 00086 int u0 = _cx3d + x0 * _cz3d / (z0 + _cz3d); 00087 int v0 = _cy3d + y0 * _cz3d / (z0 + _cz3d); 00088 int u1 = _cx3d + x1 * _cz3d / (z1 + _cz3d); 00089 int v1 = _cy3d + y1 * _cz3d / (z1 + _cz3d); 00090 line(u0, v0, u1, v1, colour); 00091 } 00092 00093 void Graphics::circle(int cx, int cy, int radius, int colour) { 00094 int x = 0; 00095 int y = radius; 00096 int d = 3 - (2 * radius); 00097 00098 while (x <= y) { 00099 _lcd->pixel(cx + x, cy + y, colour); 00100 _lcd->pixel(cx + y, cy + x, colour); 00101 _lcd->pixel(cx - x, cy + y, colour); 00102 _lcd->pixel(cx + y, cy - x, colour); 00103 _lcd->pixel(cx - x, cy - y, colour); 00104 _lcd->pixel(cx - y, cy - x, colour); 00105 _lcd->pixel(cx + x, cy - y, colour); 00106 _lcd->pixel(cx - y, cy + x, colour); 00107 00108 if (d<0) 00109 d += (4 * x) + 6; 00110 else 00111 { 00112 d += (4 * (x - y)) + 10; 00113 y--; 00114 } 00115 x++; 00116 } 00117 }
Generated on Fri Jul 15 2022 20:49:26 by
1.7.2