// Library to draw simple graphics // Simple interface using direct writing of bitmaps // // Copyright (c) 2014-2016, Felix Friedrich, Department of Computer Science, ETH Zurich // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. // * Neither the name of the ETH Zurich nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include #include #include #include "bitmap.h" #include "images.h" const float Pi = std::atan(1)*4; // width of rectangle embracing all points int Image::Width() const { return maxx -minx+1; } // height of rectangle embracing all points int Image::Height() const { return maxy-miny+1; } // draw pixel at x,y // some safety: if out of bounds of the image, do nothing void Image::Dot(const Color& color, int x, int y) { if (x<0 || y <0) return; if (x>maxx || x < minx || y < miny || y >maxy) return; bitmap(x,y) = color; } // compact Bresenham: draw line from x0,y0 to x1,y1, source Wikipedia void Image::Line(const Color& color, int x0, int y0, int x1, int y1) { int dx = abs(x1-x0), sx = x0 dy) { err += dy; x0 += sx; } /* e_xy+e_x > 0 */ if (e2 < dx) { err += dx; y0 += sy; } /* e_xy+e_y < 0 */ } } void Image::Rectangle(const Color& color, int x0, int y0, int x1, int y1) { Line(color, x0,y0,x0,y1); Line(color, x1,y0,x1,y1); Line(color, x0,y0,x1,y0); Line(color, x0,y1,x1,y1); } // compact Bresenham ellipse,source: Wikipedia void Image::Ellipse(const Color& color, int xm, int ym, int a, int b) { if (a==0 || b==0) return; int dx = 0, dy = b; /* im I. Quadranten von links oben nach rechts unten */ long a2 = a*a, b2 = b*b; long err = b2-(2*b-1)*a2, e2; /* Fehler im 1. Schritt */ do { Dot(color, xm+dx, ym+dy); /* I. Quadrant */ Dot(color, xm-dx, ym+dy); /* II. Quadrant */ Dot(color, xm-dx, ym-dy); /* III. Quadrant */ Dot(color, xm+dx, ym-dy); /* IV. Quadrant */ e2 = 2*err; if (e2 < (2*dx+1)*b2) { dx++; err += (2*dx+1)*b2; } if (e2 > -(2*dy-1)*a2) { dy--; err -= (2*dy-1)*a2; } } while (dy >= 0); while (dx++ < a) { /* fehlerhafter Abbruch bei flachen Ellipsen (b=1) */ Dot(color, xm+dx, ym); /* -> Spitze der Ellipse vollenden */ Dot(color, xm-dx, ym); } }