描述点的类Point
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassStudy2
{
class Point
{
private int x;
private int y;
private int z;
public int X
{
get { return this.x; }
set { this.x = value; }
}
public int Y
{
get { return this.y; }
set { this.y = value; }
}
public Point()
{
}
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public Point(int z)
: this(z, z)
{
}
public double DistanceFromOrgin()
{
return Math.Sqrt(x * x + y * y);
}
public double DistanceFromPoint(Point p)
{
return Math.Sqrt((this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y));
}
public static double DistanceFromPoint(Point p1, Point p2)
{
return p1.DistanceFromPoint(p2);
}
public override string ToString()
{
return "[" + x + "," + y + "]";
}
}
}
描述三角形的类Triangle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassStudy2
{
class Triangle
{
private Point p1;
private Point p2;
private Point p3;
public Point P1
{
get { return this.p1; }
set { this.p1 = value; }
}
public Point P2
{
get { return this.p2; }
set { this.p2 = value; }
}
public Point P3
{
get { return this.p3; }
set { this.p3 = value; }
}
public Triangle()
{
}
public Triangle(Point p1, Point p2, Point p3)
{
double len12 = p1.DistanceFromPoint(p2);
double len13 = p1.DistanceFromPoint(p3);
double len23 = p2.DistanceFromPoint(p3);
if (len12 + len13 <= len23 || len12 + len23 <= len13 || len13 + len23 <= len12)
{
Console.WriteLine(p1 + "," + p2 + "," + p3 + "这三点无法构成三角形,构造三角形失败!");
}
else
{
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
}
public double GetGirth()
{
double len12 = p1.DistanceFromPoint(p2);
double len13 = p1.DistanceFromPoint(p3);
double len23 = p2.DistanceFromPoint(p3);
return (len13 + len23 + len12);
}
public double GetArea()
{
//海伦公式
double len12 = p1.DistanceFromPoint(p2);
double len13 = p1.DistanceFromPoint(p3);
double len23 = p2.DistanceFromPoint(p3);
double p = (len13 + len23 + len12)/2;
return Math.Sqrt(p * (p - len12) * (p - len13) * (p - len23));
}
public override string ToString()
{
return ("此三角形的三个点为"+ p1 + ","+ p2 + "," + p3);
}
}
}
调用的类Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassStudy2
{
class Program
{
static void Main(string[] args)
{
Point p1 = new Point();
Console.WriteLine(p1);
Point p2 = new Point(-3, 15);
Console.WriteLine(p2);
Point p3 = new Point();
p3.X = 4;
p3.Y = 7;
//4.0后的新语法:构造函数调用时初始化,Point2D p3 = new Point2D{X=4, Y=7};注意要和函数的命名调用区分开来,如果这里是命名调用应该是Point3D p3 = new Point2D(x:4,y:7);
Console.WriteLine(p3.X + "," + p3.Y);
Point p4 = new Point(6);
Console.WriteLine(p4);
double dis1 = Point.DistanceFromPoint(p2, p3);
double dis2 = p2.DistanceFromPoint(p3);
double dis3 = p2.DistanceFromOrgin();
Console.WriteLine(p2 + "到" + p3 + "的距离为" + dis1);
Console.WriteLine(p2 + "到" + p3 + "的距离为" + dis2);
Console.WriteLine(p2 + "到原点的距离为" + dis3);
//下面是三角形的验证代码,作为作业
Triangle t = new Triangle(p1, p2, p3);
Console.WriteLine(t + ",面积为" + t.GetArea() + ",周长为" + t.GetGirth());
Console.ReadLine();
}
}
}