mình chưa test được tọa độ 4 đỉnh hình vuông ai biết giúp mình nha.
- Code:
using System;
class Diem
{
public Diem(float x, float y)
{
this.x = x;
this.y = y;
}
public float X
{
get { return x; }
set { x = value; }
}
public float Y
{
get { return y; }
set { y = value; }
}
float x;
float y;
}
- Code:
using System;
abstract class HinhHoc
{
public abstract float DienTich();
public abstract float ChuVi();
}
- Code:
using System;
class HinhChuNhat : HinhHoc
{
public HinhChuNhat(Diem gocTrenBenTrai,float chieuNgang,float chieuCao)
{
this.gocTrenBenTrai = gocTrenBenTrai;
this.chieuNgang = chieuNgang;
this.chieuCao = chieuCao;
}
public Diem GocTrenBenTrai
{
get { return gocTrenBenTrai; }
set { gocTrenBenTrai = value; }
}
public float ChieuNgang
{
get { return chieuNgang; }
set { chieuNgang = value; }
}
public float ChieuCao
{
get { return chieuCao; }
set { chieuCao = value; }
}
public override float ChuVi()
{
return (chieuCao + chieuNgang) * 2;
}
public override float DienTich()
{
return chieuNgang * chieuCao;
}
public Diem GetTamGiac()
{
float a = gocTrenBenTrai.X + chieuNgang / 2;
float b = gocTrenBenTrai.Y + chieuCao / 2;
return new Diem(a, b);
}
public Diem[] GetDinh()
{
Diem[] Dinh = new Diem[4];
Dinh[0]= new Diem(gocTrenBenTrai.X, gocTrenBenTrai.Y);
Dinh[1]= new Diem(gocTrenBenTrai.X, gocTrenBenTrai.X + chieuNgang);
Dinh[2]= new Diem(gocTrenBenTrai.X, gocTrenBenTrai.Y + chieuCao);
Dinh[3]= new Diem(gocTrenBenTrai.X+chieuNgang,gocTrenBenTrai.Y+chieuCao);
return Dinh;
}
Diem gocTrenBenTrai;
float chieuNgang;
float chieuCao;
}
- Code:
using System;
class HinhTron : HinhHoc
{
public HinhTron(Diem tam, float banKinh)
{
this.tam = tam;
this.banKinh = banKinh;
}
public Diem SetTam
{
set {tam=value;}
}
public float BanKinh
{
set { banKinh = value; }
}
public override float ChuVi()
{
return (float)(2*Math.PI * banKinh);
}
public override float DienTich()
{
return (float)(Math.PI * banKinh * banKinh);
}
public float DienTichHinhVuongNgoaiTiep()
{
HinhChuNhat hcn = new HinhChuNhat(new Diem(tam.X - banKinh, tam.Y - banKinh), banKinh, banKinh);
return hcn.DienTich();
}
public float ChuViHinhVuongNoiTiepHinhTron()
{
HinhChuNhat hcn = new HinhChuNhat(new Diem(tam.X - banKinh / 2, tam.Y - banKinh / 2),(float)( banKinh * Math.Sqrt(2) ),(float) (banKinh * Math.Sqrt(2)));
return hcn.ChuVi();
}
public float DienTichHinhVuongNoiTiepTrongDuongTronNoiTiepCuaCauLienTren()
{
HinhChuNhat hcn = new HinhChuNhat(new Diem(tam.X - banKinh / 4, tam.Y - banKinh / 4), (float)(banKinh * Math.Sqrt(2)/2), (float)(banKinh * Math.Sqrt(2)/2));
return hcn.DienTich();
}
public Diem ToaDoTam()
{
return tam;
}
public Diem DinhTrenBenTraiHinhVuong()
{
return new Diem(tam.X - banKinh / 4, tam.Y - banKinh / 4);
}
public float CanhHinhVuong()
{
return (float)(banKinh * Math.Sqrt(2) / 2);
}
Diem tam;
float banKinh;
}
- Code:
using System;
class HinhBinhHanh : HinhChuNhat
{
public HinhBinhHanh(Diem gocTrenBenTrai, float chieuNgang, float chieuCao,float soDoGocTrong)
:base(gocTrenBenTrai,chieuNgang,chieuCao)
{
this.soDoGocTrong = soDoGocTrong;
}
public override float ChuVi()
{
return base.ChuVi();
}
public override float DienTich()
{
return (float)(Math.Abs(base.DienTich() * Math.Sin(soDoGocTrong)));
}
float soDoGocTrong;
}
- Code:
using System;
class CungTron : HinhTron
{
public CungTron(Diem tam, float banKinh, float gocDoGocCung)
:base(tam,banKinh)
{
this.gocDoGocCung = gocDoGocCung;
}
public override float ChuVi()
{
return base.ChuVi() * gocDoGocCung / 360 + (float)(base.ChuVi() / Math.PI);
}
public override float DienTich()
{
return base.DienTich() * gocDoGocCung / 360;
}
float gocDoGocCung;
}
- Code:
using System;
class Tester
{
public static void Main()
{
// hinh chu nhat
HinhChuNhat hcn = new HinhChuNhat(new Diem(3, 4), 4, 2);
Console.WriteLine("Chu vi hinh chu nhat: "+hcn.ChuVi());
Console.WriteLine("Dien tich hinh chu nhat: "+hcn.DienTich());
Console.WriteLine("Tam hinh chu nhat: ({0},{1}) ", hcn.GetTamGiac().X, hcn.GetTamGiac().Y);
Console.WriteLine("Toa do 4 dinh hinh chu nhat");
//for (int i = 0; i < 4; i++)
// Console.WriteLine("Dinh {0} : ({1},{2})", i, hcn.GetDinh().);
//hinh tron
Console.WriteLine("");
HinhTron ht = new HinhTron(new Diem(3, 4), 4);
Console.WriteLine("Dien tich hinh tron: "+ht.DienTich());
Console.WriteLine("Chu vi hinh tron: "+ht.ChuVi());
Console.WriteLine("Dien tich hinh vuong ngoai tiep hinh tron: "+ht.DienTichHinhVuongNgoaiTiep());
Console.WriteLine("Chu vi hinh vuong noi tiep hinh tron: " + ht.ChuViHinhVuongNoiTiepHinhTron());
Console.WriteLine("Dien tich hinh vuong noi tiep hinh tron noi tiep cau lien tren: " + ht.DienTichHinhVuongNoiTiepTrongDuongTronNoiTiepCuaCauLienTren());
Console.WriteLine("Toa do tam hinh vuong cua cau lien tren: ({0},{1})",ht.ToaDoTam().X,ht.ToaDoTam().Y);
Console.WriteLine("Dinh tren ben trai hinh vuong cua cau lien tren: ({0},{1})",ht.DinhTrenBenTraiHinhVuong().X,ht.DinhTrenBenTraiHinhVuong().Y);
Console.WriteLine("Do dai canh hinh vuong cua cau lien tren: "+ht.CanhHinhVuong());
//hinh binh hanh
Console.WriteLine("");
HinhBinhHanh hbh=new HinhBinhHanh(new Diem(3,4),4,2,60);
Console.WriteLine("Dien tich hinh binh hanh: " + hbh.DienTich());
Console.WriteLine("Chu vi hinh binh hanh: " + hbh.ChuVi());
//cung tron
Console.WriteLine("");
CungTron ct = new CungTron(new Diem(3, 4), 5, 80);
Console.WriteLine("Chu vi cung tron: " + ct.ChuVi());
Console.WriteLine("Dien tich cung tron: " + ct.DienTich());
Console.ReadKey();
}
}