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 ...
随机推荐
- 安装Flask
安装Flask步骤 输入网址https://bitbucket.org/pypa/setuptools](https://bitbucket.org/pypa/setuptools,回车后进入setu ...
- Linq------各种查询语句大全
查询Title列的第一个值 string str = db.Webs.Select(p => p.Title).FirstOrDefault(); 根据ID,查询Title列的第一个值 b.We ...
- 天行API服务器地址申请
http://www.tianapi.com/ http://www.huceo.com/post/383.html
- hibernate实现有两种配置,xml配置与注释配置。
(1):xml配置:hibernate.cfg.xml (放到src目录下)和实体配置类:xxx.hbm.xml(与实体为同一目录中) <?xml version='1.0' encoding= ...
- Tomcat的目录结构、处理流程、主配置文件(server.xml)释义
参考资料: http://www.cnblogs.com/xdp-gacl/p/3744053.html http://grass51.blog.51cto.com/4356355/1123400 1 ...
- HTML 个人资料框
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Shader_2[杂]
三个shader,平滑滤波.锐化滤波和高斯模糊 http://tieba.baidu.com/p/3791791688 Unity3D研究院之自制批量修改Shader插件(五十七) http://ww ...
- curl 或 file_get_contents 获取需要授权页面的方法
原文:http://blog.csdn.net/fdipzone/article/details/44475801 红色字体部分是加上自己的注释,整理了一下. 今天因工作需要,需要用 curl / f ...
- 新浪微博客户端(35)-使用NSMutableAttributedString实现多行文本的效果
DJComposeViewController.m import "DJComposeViewController.h" #import "DJAccountTool.h ...
- 妈咪,我找到了! -- 15个实用的Linux find命令示例
妈咪,我找到了! -- 15个实用的Linux find命令示例 英文原文:Mommy, I found it! — 15 Practical Linux Find Command Examples ...