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 ...
随机推荐
- CentOS7安装hive-2.1.0
环境: CentOS7 Hadoop-2.6.4,配置两个节点:master.slave1 mysql-server 过程: 下载.解压hive-2.1.0到/usr/hadoop-2.6.4/thi ...
- wpf listbox 内的内容显示问题,需要设置里面的itemsPresenter
有时候控件并非维护本身逻辑,而是依赖于父子元素的,如了上诉的ContentPresenter,我们还有一个非常常用的ListBox控件,因为继承自ItemsControl,所以有一个ItemsPane ...
- 转换流——OutputStreamWriter类与InputStreamReader类
字节流和字符流的转换类 OutputStreamWriter:是Writer的子类,将输出的字符流变成字节流 InputStreamReader:是Reader的子类,将输入的字节流变成字符流 将字节 ...
- HTML学习笔记——head、body及简单标签
1> title标签.网站关键词.网站描述.实现百度网的跳转 2> 单标签.对标签.p标签 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...
- 一种nodejs的MVC框架
mvc会针对请求进行分发,分发一般有controller(针对模块),action(针对模块中的方法),args(请求的参数). 1.先对http请求的url进行设置,解析url中的各种参数: //c ...
- Embedding Scripts
Mono http://www.mono-project.com/docs/advanced/embedding/ http://www.mono-project.com/docs/advanced/ ...
- Linux启动盘制作
1.下载Universal-USB-Installer软件,在电脑上插入空U盘,打开软件,依下列所示步骤进行 2.点击同意 3.点击版本选择框,会出现它支持的系统镜像文件的版本,包括win7.win8 ...
- 遍历JObject
JObject jo = JObject.Parse(jsonStr); IEnumerable<JProperty> properties = jo.Properties(); fore ...
- 缺陷跟踪系统Mantis Bug Tracker
缺陷管理平台Mantis,也做MantisBT,全称Mantis Bug Tracker. 项目在github的地址:https://github.com/mantisbt/mantisbt Mant ...
- Java字节流:FilterInputStream FilterOutputStream
----------------------------------------------------------------------------------- FilterInputStrea ...