You are on page 1of 10

Hinh02 DEF.

H
#ifndef _DEF_H #define _DEF_H typedef int bool; extern const bool false, true; inline bool betw(double a, double b, double x) { return x >= a && x <= b; } inline int round(double x) { return x >= 0 ? int(x+0.5) : int(x-0.5); } #endif

DEF.CPP
#include "def.h" const bool false = 0, true = 1;

DIEM.H
#include #include #include #include <iostream.h> <graphics.h> <conio.h> "dohoa.h"

DIEM.CPP
#include #include #include #include <conio.h> <math.h> "dohoa.h" "diem.h"

class Diem { double x, y; public: Diem (double xx = 0, double yy = 0):x(xx), y(yy){} void Set(double xx, double yy) {x = xx; y = yy;} double GetX() const {return x;} double GetY() const {return y;} void Nhap(); void Xuat() const; Diem Cong(Diem b){return Diem(x+b.x,y+b.y);} Diem Tru(Diem b) {return Diem(xb.x,y-b.y);} void TinhTien(double dx, double dy) { x += dx; y += dy; } void Ve(int color = WHITE) const; double KhoangCach(Diem b); }; inline void Diem::Nhap() { cin >> x >> y; } inline void Diem::Xuat() const { cout <<"(" << x << "," << y << ")"; } inline Diem operator + (Diem a, Diem b) { return a.Cong(b); } inline Diem operator - (Diem a, Diem b) { return a.Tru(b); } inline void Diem::Ve(int color) const { textcolor(color); SetPixel(x,y); }

double sqr(double x) { return x*x; } double Diem::KhoangCach(Diem b) { return sqrt(sqr(x-b.x)+sqr(yb.y)); }

VUONG.H
#include "def.h" #include "hcn.h" class HV:public HCN { public: HV(Diem tt, double canh):HCN(tt, canh, canh){} HV(double ttx, double tty, double canh):HCN(ttx,tty,canh,canh){} HV():HCN(7,8,6,6){} char *TenLop() {return "Hinh Vuong";} };

DOHOA.CPP
#include <conio.h> #include <iostream.h> #include "dohoa.h" const int MAXX = 39, MAXY = 24; int cpx, cpy; void Moveto(int x, int y) { cpx = x; cpy = y; cout << x << " " << y << " moveto" << "\n"; } void Lineto(int x, int y) { cpx = x; cpy = y; cout << x << " " << y << " lineto" << "\n"; } void Arcto(int x, int y, double bulge) { cpx = x; cpy = y; cout << x << " " << y << " " << bulge << " arcto" << "\n"; }

VUONG.CPP
#include "vuong.h"

DOHOA.H
#ifndef _DOHOA_H #define _DOHOA_H #include <conio.h> extern const int MAXX, MAXY; inline void SetPixel(int x, int y) { if (x >= 0 && x <= MAXX && y >= 0 && y <= MAXY) { gotoxy(2*x+1, y+1); cprintf("*"); } } inline void textxy(int x, int y, char *buf) { gotoxy(2*x+1, y+1); cprintf(buf); } inline void textxy(int x, int y, char *buf, int color) { gotoxy(2*x+1, y+1); textcolor(color); cprintf(buf); } void Moveto(int x, int y); void Lineto(int x, int y); void Arcto(int x, int y, double bulge); #endif

HINH.H
#ifndef _HINH_H #define _HINH_H #include "def.h" #include "diem.h" class Hinh { public: virtual double DienTich() const = NULL; virtual void TinhTien(double dx, double dy) = NULL; virtual void Ve(int color = WHITE) const = NULL; virtual char *TenLop() {return "Hinh";} virtual bool NamTrong(Diem d) const = NULL; virtual void PhongTo(double tiLe) = NULL; virtual Diem Upl() const = NULL; virtual Diem Lwr() const = NULL; }; void VeDs(Hinh *ah[], int n, int color); void VeDs(Hinh *ah[], int n, int aColor[]); void TinhTienDs(Hinh *ah[], int n, double dx, double dy); void ViTuDs(Hinh *ah[], int n, double tiLe); double TongDienTich(Hinh *ah[], int n); #endif

HINH.CPP
#include <graphics.h> #include "hinh.h" void VeDs(Hinh *ah[], int n, int color) { for (int i = 0; i < n; i++) ah[i]->Ve(color); } void VeDs(Hinh *ah[], int n, int aColor[]) { for (int i = 0; i < n; i++) ah[i]->Ve(aColor[i]); } void TinhTienDs(Hinh *ah[], int n, double dx, double dy) { for (int i = 0; i < n; i++) ah[i]->TinhTien(dx,dy); } /* void ViTuDs(Hinh *ah[], int n, double tiLe) { for (int i = 0; i < n; i++) ah[i]->ViTu(tiLe); } */ double TongDienTich(Hinh *ah[], int n) { double S = 0; for (int i = 0; i < n; i++) S += ah[i]->DienTich(); return S; }

HCN.H
#ifndef _HCN_H #define _HCN_H #include "hinh.h" class HCN:public Hinh { Diem TrenTrai; double rong, cao; public: HCN(Diem tt, double r, double c):TrenTrai(tt), rong(r), cao(c){} HCN(double ttx, double tty, double r, double c):TrenTrai(ttx,tty), rong(r), cao(c){} HCN():TrenTrai(4,6), rong(7), cao(4){} virtual double DienTich() const {return rong*cao;} void TinhTien(double dx, double dy) {TrenTrai.TinhTien(dx,dy);} void Ve(int color = WHITE) const; bool NamTrong(Diem d) const; char *TenLop() {return "Hinh Chu Nhat";} virtual Diem Upl() const {return TrenTrai;} virtual Diem Lwr() const {return TrenTrai+Diem(rong,cao);} virtual void PhongTo(double tiLe); }; #endif

HCN.CPP
#include <conio.h> #include "dohoa.h" #include "hcn.h" void HCN::Ve(int color) const { textcolor(color); int x = round(TrenTrai.GetX()), y = round(TrenTrai.GetY()), x2 = round(TrenTrai.GetX()+rong), y2 = round(TrenTrai.GetY()+cao); for (int i = x; i <= x2; i++) SetPixel(i,y); for (int j = y+1; j < y2; j++) { SetPixel(x,j); SetPixel(x2,j); } for (i = x; i <= x2; i++) SetPixel(i,y2); } bool HCN::NamTrong(Diem d) const { double xl = TrenTrai.GetX(), xr = xl+rong, yu = TrenTrai.GetY(), yl = yu+cao; return betw(xl, xr, d.GetX()) && betw(yu, yl, d.GetY()); } void HCN::PhongTo(double tiLe) { TrenTrai.TinhTien(rong*(1-tiLe)/2, cao*(1-tiLe)/2); rong *= tiLe; cao *= tiLe; }

TGV.H
#ifndef _TGV_H #define _TGV_H #include "hinh.h" class TGV:public Hinh { Diem Dinh; double canh; public: TGV(Diem d, double c):Dinh(d), canh(c){} TGV(double x, double y, double c):Dinh(x,y),canh(c){} TGV():Dinh(2,18), canh(9){} virtual double DienTich() const {return 0.5*canh*canh;} void TinhTien(double dx, double dy) {Dinh.TinhTien(dx,dy);} void Ve(int color = WHITE) const; bool NamTrong(Diem d) const; char *TenLop() {return "Tam giac vuong";} virtual Diem Upl() const {return Dinh-Diem(0,canh);} virtual Diem Lwr() const {return Dinh+canh;} virtual void PhongTo(double tiLe); }; #endif

TGV.CPP
#include <conio.h> #include "dohoa.h" #include "tgv.h" void TGV::Ve(int color) const { textcolor(color); int x = Dinh.GetX(), y = Dinh.GetY(); for (int i = x; i <= x+canh; i++) SetPixel(i,y); for (int j = 1; j <= canh; j++) { SetPixel(x,y-j); SetPixel(x+canh-j,y-j); } } bool TGV::NamTrong(Diem d) const { int x = d.GetX()-Dinh.GetX(), y = d.GetY()-Dinh.GetY(); return betw(0, canh, x) && betw(0, canh-x, -y); } void TGV::PhongTo(double tiLe) { canh *= tiLe; }

T_HCN.CPP
#include <conio.h> #include "hcn.h" void main() { int c; clrscr(); HCN h; h.Ve(YELLOW); Diem T(10,12); T.Ve(); gotoxy(1,1); cout << (h.NamTrong(T) ? "Nam trong" : "Khong Nam trong"); do { c = getch(); if (c == 0) { switch(c = getch()) { case 75: h.TinhTien(-1,0); break; case 77: h.TinhTien(1,0); break; case 72: h.TinhTien(0,-1); break; case 80: h.TinhTien(0,1); break; } clrscr(); h.Ve(YELLOW); T.Ve(); gotoxy(1,1); cout << (h.NamTrong(T) ? "Nam trong" : "Khong Nam trong"); } } while (c!= 27); getch(); }

T_VUONG.CPP
#include <conio.h> #include "vuong.h" void main() { int c; clrscr(); HV v; HCN cn; cn.Ve(GREEN); v.Ve(YELLOW); Diem T(10,12); T.Ve(); gotoxy(1,1); cout << (cn.NamTrong(T) ? "Nam trong HCN\n" : "Khong Nam trong HCN\n"); cout << (v.NamTrong(T) ? "Nam trong HV\n" : "Khong Nam trong HV\n"); do { c = getch(); if (c == 0) { switch(c = getch()) { case 75: T.TinhTien(-1,0); break; case 77: T.TinhTien(1,0); break; case 72: T.TinhTien(0,-1); break; case 80: T.TinhTien(0,1); break; } clrscr(); cn.Ve(GREEN); v.Ve(YELLOW); T.Ve(); gotoxy(1,1); cout << (cn.NamTrong(T) ? "Nam trong HCN\n" : "Khong Nam trong HCN\n"); cout << (v.NamTrong(T) ? "Nam trong HV\n" : "Khong Nam trong HV\n"); } } while (c!= 27); getch(); }

TDM.CPP
#include <conio.h> #include "diem.h" void main() { int c; clrscr(); Diem T(10, 5); T.Ve(RED); do { c = getch(); if (c == 0) { switch(c = getch()) { case 75: T.TinhTien(-1,0); break; case 77: T.TinhTien(1,0); break; case 72: T.TinhTien(0,-1); break; case 80: T.TinhTien(0,1); break; } clrscr(); T.Ve(YELLOW); } } while (c!= 27); getch(); }

THINH.CPP
#include #include #include #include #include #include #include <stdlib.h> <stdio.h> <conio.h> <math.h> "dohoa.h" "hinh.h" "vuong.h"

#include "tgv.h" // const bool false = 0, true = 1; void main() { clrscr(); char buf[128]; char hd[] = "Bam t,g de doi hinh hien hanh, mui ten de di chuyen, +,- de thay doi kich thuoc"; Hinh *ah[3]; ah[0] = new HCN;//(5,12,8,6); ah[2] = new TGV; ah[1] = new HV; int n = 3; VeDs(ah,n,GREEN); int ch = 0; int i; for (i = 0; i < n; i++) { sprintf(buf, "Dien tich hinh %d (%s) la: %6.2lf", i, ah[i]->TenLop(), ah[i]->DienTich()); textxy(0,i,buf,WHITE); } sprintf(buf, "Tong dien tich: %6.2lf", TongDienTich(ah,n)); textxy(0,n,buf,WHITE); textxy(0,24,hd,WHITE); bool bRedraw = false; int h = 0; ch = getch(); while (ch != 27) { ch = getch(); switch(ch) { case 't': if (++h == n) h = 0; break; case 'g': if (--h < 0) h = n-1; break; case '+': ah[h]->PhongTo(1.2); bRedraw = true; break; case '-': ah[h]->PhongTo(1/1.2); bRedraw = true;

break; case 0: ch = getch(); cout << ch; switch(ch) { case 72: ah[h]->TinhTien(0,-1); bRedraw = true; break; case 80: ah[h]->TinhTien(0,1); bRedraw = true; break; case 75: ah[h]->TinhTien(-1,0); bRedraw = true; break; case 77: ah[h]->TinhTien(1,0); bRedraw = true; break; } } if (bRedraw) { clrscr(); VeDs(ah,n,GREEN); for (i = 0; i < n; i++) { sprintf(buf, "Dien tich hinh %d (%s) la: %6.2lf", i, ah[i]>TenLop(), ah[i]->DienTich()); textxy(0,i,buf,WHITE); } sprintf(buf, "Tong dien tich: %6.2lf", TongDienTich(ah,n)); textxy(0,n,buf); textxy(0,24,hd,WHITE); bRedraw = false; } } getch(); }

You might also like