C# Program To Calculate The Distance Between Two Points in 2d and 3d
C# Program To Calculate The Distance Between Two Points in 2d and 3d
1.0 INTRODUCTION.................................................................................................................................2
2.0 SOURCE CODE...................................................................................................................................3
2.1 CLASS COORDINATETEST...................................................................................................................3
2.2 CLASS CLASS2DPOINT........................................................................................................................4
2.3 CLASS CLASS3DPOINT........................................................................................................................5
3.0 DESIGN................................................................................................................................................7
3.1 UML...................................................................................................................................................7
3.2 DATA DICTIONARY.............................................................................................................................8
3.3 PSEUDO CODE...................................................................................................................................9
3.4 FLOWCHART....................................................................................................................................10
4.0 TESTING............................................................................................................................................13
4.1 TEST PLAN........................................................................................................................................13
4.2 TEST LOG..........................................................................................................................................14
4.3 PRINT SCREEN..................................................................................................................................15
5.0 CONCLUSION...................................................................................................................................17
6.0 REFERENCE......................................................................................................................................18
Appendix I USER GUIDE........................................................................................................................19
1
1.0 INTRODUCTION
Technology today has become relevant in every sphere of life with its applications bringing
about an ease in the way we carry out our day to day activities. This has led to an introduction of
computerization in most regular activities such as mathematical calculations which in turn brings
about a greater rate of accuracy and efficiency in these so called mathematical calculations. This
program is console based and is meant to receive input from the user of the x, y and z co-
ordinates of two points and then go ahead to calculate the distance between these two points in
both 2D and 3D, and to plot these points on the console
2
2.0 SOURCE CODE
namespace ConsoleApplication1
{
class CoordinateTest
{
static void Main(string[] args)
{
int[] num;
num = new int[6];
3
object1 = object1 - object4;
Console.ReadLine();
}
}
}
namespace ConsoleApplication1
{
public class Class2DPoint
{
protected int x;
protected int y;
public Class2DPoint()
{
this.x = 0;
this.y = 0;
}
public Class2DPoint(int x, int y)
{
this.x = x;
this.y = y;
}
public double showDistance(Class2DPoint secondPoint)
{
int x1 = this.x;
int y1 = this.y;
int x2 = secondPoint.x;
int y2 = secondPoint.y;
double distance = Math.Sqrt(Math.Pow((x2 - x1), 2.0) + Math.Pow((y2 - y1),
2.0));
Console.WriteLine(distance);
return distance;
}
public static Class2DPoint operator -(Class2DPoint object1, Class2DPoint object2)
{
Class2DPoint OO = new Class2DPoint();
4
OO.x=(object1.x - object2.x) * 2;
OO.y=(object1.y - object2.y) * 2;
return OO;
}
namespace ConsoleApplication1
{
public class Class3DPoint : Class2DPoint
{
private int z;
public Class3DPoint() : base()
{
z = 0;
5
}
public Class3DPoint(int x, int y, int z)
: base(x, y)
{
this.z = z;//IU
}
public static Class3DPoint operator *(Class3DPoint object1, Class3DPoint object2)
{
Class3DPoint TT= new Class3DPoint();
TT.x=(object1.x - object2.x) * 2;
TT.y=(object1.y - object2.y) * 2;
TT.z=(object1.z - object2.z) * 2;
return TT;
}
public void drawGraph(Class3DPoint obj, Class3DPoint obj1)
{
char[,] Andrew;
Andrew = new char[10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Andrew[i, j] = '.';
}
}
Andrew[obj.x, obj.y] = 'A';
Andrew[obj1.x, obj1.y] = 'B';
6
}
}
}
3.0 DESIGN
3.1 UML
The UML of this program defines the distance between two points. The entities of the UML are
the classes.
Class2DPoint Class3DPoint
8
3.3 PSEUDO CODE
Start
Console.ReadLine();
Console.ReadLine();
Console.ReadLine();
Console.ReadLine();
Console.ReadLine();
Console.ReadLine();
End
9
3.4 FLOWCHART
start
D
B
ConsolePrintLine
ConsoleWriteline“WELCOME
“Enter the TO 2Dof
value AND 3D
z for
POINT
2nd 3DCALCULATOR”
constructor”
ConsoleWriteline
“THIS PROGRAM “Enter the value of z for
CALCULATES”
1st 3D constructor”
"THE DISTANCE IN 2D AND 3D
BETWEEN ANY TWO POINTS”
Console.ReadLine();
Console.ReadLine();
C
END
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
4.0 TESTING
3 Test input for 1st y “5” Program should be able to receive pass 19/08/2010
co-ordinate integer input “5”
4 Test input for 1st z “4” Program should be able to receive pass 19/08/2010
co-ordinate integer input “4”
5 Test input for 2nd x “5” Program should be able to receive pass 19/08/2010
co-ordinate integer input “5”
6 Test input for 2nd y “7” Program should be able to receive Pass 19/08/2010
co-ordinate integer input “7”
7 Test input for 2nd z “6” Program should be able to receive pass 19/08/2010
co-ordinate integer input “6”
8 Test for 2DPoints, “ calculated Program should be able to display pass 19/08/2010
Distance between 2Dpoints distance the answer of the calculated
A and B answer” distance between A and B
9 Test for 3DPoints, “ calculated distance Program should be to calculate and Pass 19/082010
Distance between 3Dpoints answer” display the answer of the both
A and B 2Dpoints and 3Dpointd.
10 Test for display of “2Dpoints and Program should be able to display pass 19/08/2010
2dpoints and 3Dpoints answer on the result of 2Dpoints and
3dpoints on graph. graph” 3Dpoints on the graph.
97
4.2 TEST LOG
98
4.3 PRINT SCREEN
Fig2: The input of the x, y and z coordinates for the 2D and 3D points
99
Fig3: The graph for the 2D and 3D points are plotted and the distance between the points are calculated
100
5.0 CONCLUSION
This program has been successfully coded, compiled and executed which has been very impressive and
will go ahead to ease the extra work caused by plotting of graphs for simple calculations and it has
demonstrated some reasonable programming logics like inheritance, polymorphism, and the use of
objects which are very good OOP practices.
101
6.0 REFERENCE
Microsoft corporation, 2008. Visual C# 2008 Express Edition.[e-book]
102
Appendix I USER GUIDE
This program is meant to simply calculate and displaying in a graph, the co-ordinate of A and B of
2Dpoint and 3Dpoint.
• Key in the value of x for first 2Dpoint (note: value should be < integer 10 and > integer 1)
• Key in the value of y for first 2Dpoint (note: value should be < integer 10 and > integer 1)
• Key in the value of z for first 3Dpoint (note: value should be < integer 10 and > integer 1)
• Key in the value of x for second 2Dpoint (note: value should be < integer 10 and > integer 1)
• Key in the value of y for second 2Dpoint (note: value should be < integer 10 and > integer 1)
• Key in the value of z for second 3Dpoint (note: value should be < integer 10 and > integer 1)
• The A and B co-ordinate will be displayed on the graph, showing their distance and position
apart.
• The distance between the 2Dpoints will be displayed below the graph
• The distance between the 3Dpoints will be displayed below that of 2Dpoints
103