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