c#中如何编写一个计算空间中两个点距离的成员方法,(注:这两个点的空间坐标是这个累的实例
2个回答
展开全部
创建控制台工程,代码复制进去。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.IO;
namespace ConsoleApplication1
{
public class Coordinate
{
int x;
public int X
{
get { return x; }
set { x = value; }
}
int y;
public int Y
{
get { return y; }
set { y = value; }
}
public Coordinate(int a, int b)
{
this.X = a;
this.Y = b;
}
public static void JiSuanDistance(Coordinate a,Coordinate b)
{
double m = (a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y);
Console.WriteLine("两点间距离为:"+System.Math.Sqrt(m));
}
}
class Program
{
static void Main(string[] args)
{
Coordinate r = new Coordinate(100,80);
Coordinate t = new Coordinate(90,70);
Coordinate.JiSuanDistance(r,t);
Console.Read();
}
}
}
展开全部
class Point
{
public double X { get; private set; }
public double Y { get; private set; }
// it1:创建构造函数
public Point(double X,double Y)
{
this.X = X;
this.Y = Y;
}
// it2:重写Object类的ToString()方法
public override string ToString()
{
return X.ToString() + "," + Y.ToString();
}
// it3:创建静态的CalculteDistance()函数
public static double CalculteDistance(Point a,Point b)
{
double m = (a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y);
return System.Math.Sqrt(m);
}
// 创建两个点
Point a = new Point(3, 0);
Point b = new Point(0, 4);
// 两个点之间的距离
double result = Point.CalculteDistance(a, b);
// 输出结果
Console.WriteLine("{0}和{1}之间的距离为{2}", a.ToString(), b.ToString(), result);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询