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 ...
随机推荐
- PHPCMSv9首页显示分页点击下一页跳转链接出现错误,跳转到后台的解决方案
1 引用页写为 {pc:content action="lists" catid="10" order="updatetime DESC" ...
- BufferedReader类
BufferedReader类用于从缓冲区中读取内容,多有的输入字节数据都将放在缓冲区中. BufferedReader中定义的构造方法只能接收字符输入流的实例,所以必须使用字符输入流和字节输入流的转 ...
- OWIN support for the Web API 2 and MVC 5 integrations in Autofac
Currently, in the both the Web API and MVC frameworks, dependency injection support does not come in ...
- State Threads——异步回调的线性实现
State Threads——异步回调的线性实现 原文链接:http://coolshell.cn/articles/12012.html 本文的标题看起来有点拗口,其实State Threads库就 ...
- MVC中的@section
在前文<MVC中 _ViewStart _Layout Index三个页面中的加载顺序> 中另外指定母版页 Layout.cshtml时...遇到了这个问题.. 报错: 以下各节已定义,但 ...
- TFS2012 安装 配置笔记
TFS2012安装 具体请看文档.. http://yunpan.cn/cmt4X6S7TjEgq 访问密码 464e TFS2012配置 环境:VS2012 SQL2008 T ...
- VisualStudio基本使用(1)-显示行号
"工具"-"选项"-"文本编辑器"-"C/C++"-"常规",勾选"行号"复选框 ...
- JAVA日期加减运算
1.用java.util.Calender来实现 Calendar calendar=Calendar.getInstance(); calendar.setTime(new Date()) ...
- 父容器的flowover:hidden 必须配合父容器的宽高height width才能生效
有时候 给父容器 加上了 flowover:hidden 这个css后, 其中的子元素为什么仍然会跑出来? 解决的根本方法就是要给 父容器 加上具体的一个宽度, 或高度. (而这个宽度和高度, 其实你 ...
- ZOJ3802 Easy 2048 Again (状压DP)
ZOJ Monthly, August 2014 E题 ZOJ月赛 2014年8月 E题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...