0% found this document useful (0 votes)
54 views10 pages

Hinh01 Def.H: #Ifndef #Define Typedef Int Bool Extern Const Bool False True Inline Bool Double Double Double Return

This document contains code definitions and implementations for classes that represent geometric shapes like points, rectangles, squares, and right triangles in C++. It includes header files that define the classes and cpp files that implement class methods. The code allows creating shape objects, drawing them to the screen, calculating their areas, checking if a point is contained within a shape, and manipulating the shapes by moving, resizing, or changing their colors. Test programs are provided that create shapes, draw and manipulate them, and check properties like containment of a point.

Uploaded by

BlueNote0309
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views10 pages

Hinh01 Def.H: #Ifndef #Define Typedef Int Bool Extern Const Bool False True Inline Bool Double Double Double Return

This document contains code definitions and implementations for classes that represent geometric shapes like points, rectangles, squares, and right triangles in C++. It includes header files that define the classes and cpp files that implement class methods. The code allows creating shape objects, drawing them to the screen, calculating their areas, checking if a point is contained within a shape, and manipulating the shapes by moving, resizing, or changing their colors. Test programs are provided that create shapes, draw and manipulate them, and check properties like containment of a point.

Uploaded by

BlueNote0309
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Hinh01 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";} virtual void GianNgang(double tiLe); virtual void GianDoc(double tiLe); };

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

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"; }

VUONG.CPP
#include <math.h> #include "vuong.h" void HV::GianNgang(double tiLe) { PhongTo(sqrt(tiLe)); } void HV::GianDoc(double tiLe) { PhongTo(sqrt(tiLe)); }

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);

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"; }

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; virtual void GianNgang(double tiLe) = NULL; virtual void GianDoc(double tiLe) = 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); virtual void GianNgang(double tiLe); virtual void GianDoc(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; } void HCN::GianNgang(double tiLe) { TrenTrai.TinhTien(rong*(1-tiLe)/2, 0); rong *= tiLe; } void HCN::GianDoc(double tiLe) { TrenTrai.TinhTien(0, cao*(1tiLe)/2); 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"

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(2,12,8,6); ah[2] = new HCN; ah[1] = new HV; int am[] = {WHITE,YELLOW,YELLOW}; int n = 3; VeDs(ah,n,am); 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; while (ch != 27) { ch = getch(); switch(ch) { case 't': am[h] = YELLOW; if (++h == n) h = 0; am[h] = WHITE; bRedraw = true; break; case 'g': am[h] = YELLOW; if (--h < 0) h = n-1; am[h] = WHITE; bRedraw = true; 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,am); 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