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 ...
随机推荐
- easyUI增加视图分组的办法
1.在JSP头文件中引入如下代码 <script type="text/javascript" src="${pageContext.request.context ...
- win 2012 关闭IE增强设置
- 9月23日JavaScript作业----用DIV做下拉列表
例题二.用div做下拉列表 <title>无标题文档</title> <style type="text/css"> *{ margin:0px ...
- Runner站立会议08
会议时间:2016.4.27 21.10~21.25 地点:基教负一层 今天:看日历的代码,网上下的,没有注释 明天:继续看代码 困难:代码看不懂 会议照片: 燃尽图:
- StringBuffer类
String的内容一旦声明则不可改变,如果改变,则改变的肯定是String的引用地址. 如果一个字符串要被经常改变,则就必须使用StringBuffer类. 在String类中可以通过“+”进行字符串 ...
- Shared Library Search Paths
在使用CodeLite编译动态库的时候,可以通过在Linker > Linker Options中添加: -install_name @executable_path/libXXX.so 的方式 ...
- iOS监听键盘事件
#pragma mark - view life cycle - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter def ...
- IE安全分析
IE安全问题,这个话题似乎很古老了,但是问题又是层出不穷~ 对于IE的安全,我个人认为有两点需要关注,一个是上网的安全,二个是IE解析代码的安全 对于IE上网安全,这是最基本的,也是最常用的了 附上一 ...
- Nginx for Windows 使用笔记
一.常见启动错误 1. "No mapping for the Unicode character exists in the target multi-byte code page&quo ...
- 清除SQL server2008 记住的用户名和密码
删除以下文件即可: C:\Users\%username%\AppData\Roaming\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStud ...