C # Object-oriented thinking calculates distance between two points

Source: Internet
Author: User
Tags xdiff

The topic for calculating the distance between two points.

The process-oriented way of thinking, two points of the difference between the horizontal axis, the difference between the square sum, and then open with, get two points between the distance.

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 ();}}}    

Object-oriented thinking, the topic, two points between the straight line distance, the noun includes point, line, distance, first we construct a point class.

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);}}}    

  

Then instantiate the P1,P2 two points in the Programe.cs and calculate the distance

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 # Object-oriented thinking calculates distance between two points

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.