#!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # 7_3a.c 7_3c.c 7_3d.c makefile nshape.c tst.cmp # # To extract the files from this shell archive file simply # create a directory for this file, move the archive file # to it and enter the command # # sh filename # # The files will be extracted automatically. # Note: Do not use csh. # # Archive created: Mon Jul 30 23:10:23 EDT 1990 # echo x - 7_3a.c sed 's/^X//' > 7_3a.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // exercise 7.3 class triangle: public shape { point sw, se, top; int UP() { return (top.y >= se.y) ? ( (top.y >= sw.y) ? top.y : sw.y ) : (se.y >= sw.y) ? se.y : sw.y; } int RIGHT() { return (top.x >= se.x) ? ( (top.x >= sw.x) ? top.x : sw.x ) : (se.x >= sw.x) ? se.x : sw.x; } int DOWN() { return (top.y <= se.y) ? ( (top.y <= sw.y) ? top.y : sw.y ) : (se.y <= sw.y) ? se.y : sw.y; } int LEFT() { return (top.x <= se.x) ? ( (top.x <= sw.x) ? top.x : sw.x ) : (se.x <= sw.x) ? se.x : sw.x; } public: triangle(point a, point b, point c) { sw = a; top = b; se = c; } void move(int x, int y) { sw.x += x; sw.y += y; top.x += x; top.y += y; se.x += x; se.y += y; } void draw() { put_line(sw, top); put_line(top, se); put_line(se, sw); } point north() { return point((LEFT() + RIGHT()) / 2, UP()); } point south() { return point((LEFT() + RIGHT()) / 2, DOWN()); } point east() { return point(LEFT(), (UP() + DOWN()) / 2); } point west() { return point(RIGHT(), (UP() + DOWN()) / 2); } point swest() { return point(LEFT(), DOWN()); } point seast() { return point(RIGHT(), DOWN()); } point nwest() { return point(LEFT(), UP()); } point neast() { return point(RIGHT(), UP()); } }; !EOF! ls -l 7_3a.c echo x - 7_3c.c sed 's/^X//' > 7_3c.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // exercise 7.3 #undef line /* DELETE */ #include class circle: public shape { point center; int radius; public: circle(point a, int r) { center = a; radius = r; } circle(point a, point b) { center = a; radius = (int) sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } void move(int a, int b) { center.x += a; center.y += b; } void draw(); point north() { return point(center.x + radius, center.y); } point south() { return point(center.x - radius, center.y); } point east() { return point(center.x, center.y + radius); } point west() { return point(center.x, center.y - radius); } point neast() { return point(center.x + radius, center.y + radius); } point nwest() { return point(center.x - radius, center.y + radius); } point seast() { return point(center.x - radius, center.y + radius); } point swest() { return point(center.x - radius, center.y - radius); } }; !EOF! ls -l 7_3c.c echo x - 7_3d.c sed 's/^X//' > 7_3d.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // exercise 7.3 // draw a circle // draw a point, offset by the location // of the center of the circle inline void put_circle_point(int x, int y, point center) { put_point(x + center.x, y + center.y); } // draw the point in all 8 octants static void draw_circle_points(point pt, point center) { put_circle_point(pt.x, pt.y, center); put_circle_point(pt.y, pt.x, center); put_circle_point(pt.y, -pt.x, center); put_circle_point(pt.x, -pt.y, center); put_circle_point(-pt.x, -pt.y, center); put_circle_point(-pt.y, -pt.x, center); put_circle_point(-pt.y, pt.x, center); put_circle_point(-pt.x, pt.y, center); } // draw a circle using Bresenham's algorithm // as described by Foley and Van Dam, 1982 void circle:: draw() { point pt(0, radius); int d = 3 - 2 * radius; while (pt.x < pt.y) { draw_circle_points(pt, center); if (d < 0) d += 4 * pt.x + 6; else { d += 4 * (pt.x - pt.y) + 10; pt.y--; } pt.x++; } if (pt.x == pt.y) draw_circle_points(pt, center); } !EOF! ls -l 7_3d.c echo x - makefile sed 's/^X//' > makefile << '!EOF!' CC= CC -I. -I../shape -I../../CC CFLAGS= +i -g SHP= ../shape all: tst OBJ= nshape.o $(SHP)/shape.o $(SHP)/myshape.o $(SHP)/slist.o $(SHP)/put_line.o $(SHP)/screen.o tst: $(OBJ) $(CC) $(OBJ) -o tst nshape.o: nshape.c $(SHP)/shape.h 7_3a.c 7_3c.c 7_3d.c $(CC) $(CFLAGS) -c nshape.c CMP= tst.cmp OUT= tst.out tst.out: tst ; tst > tst.out test: all $(CMP) $(OUT) cmp tst.out tst.cmp @echo test done !EOF! ls -l makefile echo x - nshape.c sed 's/^X//' > nshape.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #define line Xline #include "shape.h" #include "7_3a.c" /* class triangle */ #include "7_3c.c" /* class circle */ #include "7_3d.c" /* circle::draw() */ main() { screen_init(); shape* p1 = new triangle(point(0,0),point(10,20),point(5,5)); shape* p2 = new circle(point(20,15),17); shape_refresh(); p1->move(10,5); p2->move(5,10); shape_refresh(); screen_end(); return 0; } !EOF! ls -l nshape.c echo x - tst.cmp sed 's/^X//' > tst.cmp << '!EOF!' ********* ** ** ** ** ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * ** * * ** * * ** * * ** * ** * * *** * *** * *** * * ** * * ** * * * * * ** * * ** * * * ** ** * ** ** ********* ** ** ** ** ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * ** * * ** * * ** * * ** * * * * * * ** * ** * * * * * * ** ** * * ** ** * * ** ** ** ********* ** * * !EOF! ls -l tst.cmp # The following exit is to ensure that extra garbage # after the end of the shar file will be ignored. exit 0