C#面向对象思想计算两点之间距离

题目为计算两点之间距离。

面向过程的思维方式,两点的横坐标之差,纵坐标之差,平方求和,再开跟,得到两点之间距离。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Classes_2_point_distance
{
    class Program
    {
        static void Main(string[] args)
        {
            int x1 = -1;
            int y1 = -1;
            int x2 = int.Parse(Console.ReadLine());
            int y2 = int.Parse(Console.ReadLine());

            int xdiff = x2 - x1;
            int ydiff = y2 - y1;
            double distance = Math.Sqrt(xdiff * xdiff + ydiff * ydiff);

            Console.WriteLine(distance);
            Console.ReadKey();

        }
    }
}

  面向对象的思路,题目中,两点间直线距离,名词包括点、直线、距离,首先我们构造一个点类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Classes_2_point_distance
{
    class Point
    {
        private int x;
        private int y;

        public Point()
        {
            x = -1;
            y = -1;
        }

        public Point(int h, int z)
        {
            x = h;
            y = z;
        }
        public double Distance(Point p)
        {
            int xdiff = x - p.x;
            int ydiff = y - p.y;

            return Math.Sqrt(xdiff * xdiff + ydiff * ydiff);

        }
    }
}

  

然后再Programe.cs中实例化p1,p2两个点,计算距离

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Classes_2_point_distance
{
    class Program
    {
        static void Main(string[] args)
        {
            //int x1 = -1;
            //int y1 = -1;
            int x2 = int.Parse(Console.ReadLine());
            int y2 = int.Parse(Console.ReadLine());

            //int xdiff = x2 - x1;
            //int ydiff = y2 - y1;
            //double distance = Math.Sqrt(xdiff * xdiff + ydiff * ydiff);

            //Console.WriteLine(distance);
            //Console.ReadKey();

            Point p1 = new Point();
            Point p2 = new Point(x2, y2);
            double distance = p1.Distance(p2);
            Console.WriteLine(distance);
            Console.ReadKey();

        }
    }
}

  

时间: 2024-08-02 15:14:16

C#面向对象思想计算两点之间距离的相关文章

C# 通过GPS坐标,计算两点之间距离

之前在网上有很多这种计算的,但是代码都不怎么全.经过多方打听查询.找到完整代码.现将代码共享给大家. 有需要者觉得有用者欢迎使用.觉得用或简单的高手,请绕. public static double GetDistance(double lat1, double lng1, double lat2, double lng2) { double radLat1 = rad(lat1); double radLat2 = rad(lat2); double a = radLat1 - radLat2

JAVA实现求一点到另两点连线的距离,计算两点之间的距离

直接上代码 /** *计算两点之间距离 */ public static double getDistance(Point start,Point end){ double lat1=start.getX().doubleValue(); double lat2=end.getX().doubleValue(); double lon1=start.getY().doubleValue(); double lon2=end.getY().doubleValue(); return Math.sq

利用结构类型的相关知识计算两点之间的距离

#include<stdio.h>#include<stdlib.h>#include<math.h> struct point{ /*点的结构类型名*/ float x; /*横坐标*/ float y; /*纵坐标*/ }; struct point readPoint(); /*函数原型声明*/float distance(struct point p1,struct point p2);/*主函数*/ int main(void){ struct point a

2D和3D空间中计算两点之间的距离

自己在做游戏的忘记了Unity帮我们提供计算两点之间的距离,在百度搜索了下. 原来有一个公式自己就写了一个方法O(∩_∩)O~,到僵尸到达某一个点之后就向另一个奔跑过去 /// <summary> /// 3维中如何计算两点之间的距离 /// </summary> /// <param name="p1"></param> /// <param name="p2"></param> /// &l

openlayer3计算两点之间的距离

openlayer3计算两点之间的距离 对应的openlayers的版本为3.7. 主要用的接口是ol.Sphere.haversineDistance([x1,y1],[x2,y2]): 4326坐标系中计算两点距离的方式为: var wgs84Sphere = new ol.Sphere(6378137); wgs84Sphere.haversineDistance(C1,C2); 示例为: var wgs84Sphere = new ol.Sphere(6378137); wgs84Sph

使用友元函数计算两点之间的距离

#include <iostream> #include <cmath> using namespace std; class CPoint//点类 { private: double x;//横坐标 double y;//纵坐标 public: //使用初始化表初始化数据成员 CPoint(double xx=0,double yy=0):x(xx),y(yy){} //定义友元函数用于计算两点之间的距离 friend double Distance(CPoint &p1

sql server2008根据经纬度计算两点之间的距离

--通过经纬度计算两点之间的距离 create FUNCTION [dbo].[fnGetDistanceNew] --LatBegin 开始经度 --LngBegin 开始维度 --29.490295,106.486654,29.615467, 106.581515 (@LatBegin1 varchar(128), @LngBegin1 varchar(128),@location varchar(128)) Returns real AS BEGIN --转换location字段,防止字段

计算两点之间的角度的代码

+ ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 /// 离心率 /// 返回两点间的角度 private double PointToAngle(Point AOrigin, Point APoint, double AEccentricity) { if (APoint.X == AOrigin.X) if (APoint.Y > AOrigin.Y) return Math.PI * 0.5;

计算两点之间的距离,两点之间的斜率(角度)--秀清

// // ViewController.m // 勾股定理 // // Created by 张秀清 on 15/6/8. // Copyright (c) 2015年 张秀清. All rights reserved. // #import "ViewController.h" //角度转弧度 #define degreesToradian(x) (M_PI*x/180.0) //弧度转角度 #define radiansToDegrees(x) (180.0*x/M_PI) @i