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(); }
}
}
C#面向对象思想计算两点之间距离的更多相关文章
- C# 通过GPS坐标,计算两点之间距离
之前在网上有很多这种计算的,但是代码都不怎么全.经过多方打听查询.找到完整代码.现将代码共享给大家. 有需要者觉得有用者欢迎使用.觉得用或简单的高手,请绕. public static double ...
- sql server2008根据经纬度计算两点之间的距离
--通过经纬度计算两点之间的距离 create FUNCTION [dbo].[fnGetDistanceNew] --LatBegin 开始经度 --LngBegin 开始维度 --29.49029 ...
- 2D和3D空间中计算两点之间的距离
自己在做游戏的忘记了Unity帮我们提供计算两点之间的距离,在百度搜索了下. 原来有一个公式自己就写了一个方法O(∩_∩)O~,到僵尸到达某一个点之后就向另一个奔跑过去 /// <summary ...
- java 根据经纬度坐标计算两点的距离算法
/** * @Desc 根据经纬度坐标计算两点的距离算法<br> * @Author yangzhenlong <br> * @Data 2018/5/9 18:38 */ p ...
- (转)c# math 计算两点之间的角度公式
计算两点之间的角度公式是: 假设点一(X1,Y1),点二(X2,Y2) double angleOfLine = Math.Atan2((Y2 - Y1), (X2 - X2)) * 180 / Ma ...
- 求两点之间距离 C++
求两点之间距离(20 分) 定义一个Point类,有两个数据成员:x和y, 分别代表x坐标和y坐标,并有若干成员函数. 定义一个函数Distance(), 用于求两点之间的距离.输入格式: 输入有两行 ...
- js通过经纬度计算两点之间的距离
最近这几天在做地图的时候,获取到目的地经纬度和当前所在位置的经纬度,通过这几个参数,用js代码就能获取到这两点之间的直线距离: function (lat1, lng1, lat2, lng2) { ...
- IOS计算两点之间的距离
//广州经纬度 CLLocationCoordinate2D guangZhouLocation; guangZhouLocation.latitude = 23.20; guangZhouLocat ...
- Java 根据经纬度计算两点之间的距离
package xxx.driver.business.utils; /** * <p>Represents a point on the surface of a sphere. (Th ...
随机推荐
- 日志分析 第七章 安装grafana
grafana依赖mysql存储数据,首先需要安装mysql 安装mysql 解压 # groupadd mysql # useradd -s /sbin/nologin -g mysql mysql ...
- Java web 使用页面压缩
借助类,相关依赖: <!-- https://mvnrepository.com/artifact/net.sourceforge.pjl-comp-filter/pjl-comp-filter ...
- OpenGL Tutorial
https://open.gl https://www.processing.org/tutorials/pshader/
- Starling Tutorial
http://www.hsharma.com/tutorials/starting-with-starling-ep-1-intro-setup/
- push submodule
git status git add sparx git commit -m "message" git push
- 关于webpack抛出对象到全局的问题
一般情况下,我们用webpack的时候.大多是用在单页应用上. 单是,某些情况下,我们用来做多页面的时候,有的时候,会需要在html内嵌 <script>,比如说,这个页面是服务端渲染的, ...
- Effective Objective-C 2.0 — 第四条:多用类型常量,少用#define预处理指令
第四条:多用类型常量,少用#define预处理指令 使用#define 预处理的坏处:定义出来的常量没有类型信息,编译器只是会在编译前据此执行查找与替换操作.即使有人重新定义了常量值,编译器也不会产生 ...
- python快排算法
通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列. ...
- Android中Java与JavaScript之间交互(转)
Android代码: package com.fyfeng.testjavascript; import android.app.Activity; import android.content.In ...
- vim 树形目录插件NERDTree安装及简单用法
转自: http://blog.csdn.net/love__coder/article/details/6659103 1,安装NERDTree插件 先下载,官网:http://www.vim.or ...