100% found this document useful (1 vote)
2K views103 pages

C# Program To Calculate The Distance Between Two Points in 2d and 3d

Uploaded by

chineduiroanyah
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views103 pages

C# Program To Calculate The Distance Between Two Points in 2d and 3d

Uploaded by

chineduiroanyah
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 103

Table of Contents

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

2.1 CLASS COORDINATETEST


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class CoordinateTest
{
static void Main(string[] args)
{

int[] num;
num = new int[6];

Console.WriteLine("\t********* WELCOME TO THE COORDINATE TEST PROGRAM


***********");
Console.WriteLine("\t***************** THIS PROGRAM CALCULATES
*******************");
Console.WriteLine("\t***** THE DISTANCE IN 2D AND 3D BETWEEN ANY TWO POINTS
******\n\n\n");

Console.Write("\nEnter value of x for 1st 2D Constructor : ");


int x1= Convert.ToInt32(Console.ReadLine());
num[0] = x1;

Console.Write("Enter value of y for 1st 2D Constructor : ");


int y1 = Convert.ToInt32(Console.ReadLine());
num[1] = y1;

Console.Write("Enter value of z for 1st 3D Constructor : ");


int z1 = Convert.ToInt32(Console.ReadLine());
num[2] = z1;

Console.Write("Enter value of x for 2nd 2D Constructor : ");


int x2 = Convert.ToInt32(Console.ReadLine());
num[3] = x2;

Console.Write("Enter value of y for 2nd 2D Constructor : ");


int y2 = Convert.ToInt32(Console.ReadLine());
num[4] = y2;

Console.Write("Enter value of z for 2nd 3D Constructor : ");


int z2= Convert.ToInt32(Console.ReadLine());
num[5] = z2;

Class2DPoint object1 = new Class2DPoint(num[0], num[1]);


Class2DPoint object2 = new Class2DPoint(num[3], num[4]);
Class3DPoint object3 = new Class3DPoint(num[0], num[1], num[2]);
Class3DPoint object4 = new Class3DPoint(num[3], num[4], num[5]);

Console.WriteLine("The Graphical representation of point in graph\n\n");


object2.drawGraph(object3, object4);

3
object1 = object1 - object4;

Console.Write("\n\n2DPoints, Distance between A and B is: ");


object1.showDistance(object2);

object2 = object3 * object4;


Console.Write("\n3DPoints, Distance between A and B is: ");
object3.showDistance(object4);

Console.Write("\nPress the Enter key to Exit");

Console.ReadLine();
}
}
}

2.2 CLASS CLASS2DPOINT


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

public void drawGraph(Class2DPoint obj, Class2DPoint 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';

for (int k = 9; k >= 0; k--)


{
Console.Write(k + 1);
Console.Write("\t");
for (int l = 0; l < 10; l++)
{
Console.Write(Andrew[k, l]);
Console.Write(" ");
}
Console.WriteLine();
}
Console.Write("\t");
for (int k = 0; k < 10; k++)
{
Console.Write(k + 1);
Console.Write(" ");
}
Console.WriteLine();
}
}
}

2.3 CLASS CLASS3DPOINT


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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';

for (int k = 9; k >= 0; k--)


{
Console.Write(k + 1);
Console.Write("\t");
for (int l = 0; l < 10; l++)
{
Console.Write(Andrew[k, l]);
Console.Write(" ");
}
Console.WriteLine();
}
Console.Write("\t");
for (int k = 0; k < 10; k++)
{
Console.Write(k + 1);
Console.Write(" ");
}
Console.WriteLine();
}
public double showDistance(Class3DPoint secondPoint)
{
int x1 = this.x;
int y1 = this.y;
int z1 = this.z;
int x2 = secondPoint.x;
int y2 = secondPoint.y;
int z2 = secondPoint.z;
double distance = System.Math.Sqrt(x + y + z);
Console.WriteLine(distance);
return distance;

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

#x: int -z (int)


#y: int 7
+ Class2DPoint()
+ Class2DPoint() +Class2DPoint(intx, int y): int
+Class2DPoint(): int + showDistance(Class3DPoint secondPoint): double
3.2 DATA DICTIONARY
- = {private}
# = {protected}
+ = {public}
() = {constructor}
Int = {32bit numeric}
String = {alphabet + numeric + (ASCII code)}
x = {value of 1st Class2DPoint constructor + value of 2 nd Class2DPoint constructor}
y = {value of 1st Class3DPoint constructor + value of 2 nd Class3DPoint constructor}
z = {value of Class3DPoint }
Class2Dpoint = {distance + position + 2D object}
Class3Dpoint = {distance + position + 3D object}
drawGraph = {graph + distance of Class2DPoint + distance of Class3DPoint}
showDistance ={ display distance of Class2DPoint + Class3Dpoint}
Inheritance = { attributes of Class2DPoint + extra attributes of Class3DPoint

8
3.3 PSEUDO CODE
Start

ConsolePrintLine “WELCOME TO 2D AND 3D POINT CALCULATOR”

“THIS PROGRAM CALCULATES”

"THE DISTANCE IN 2D AND 3D BETWEEN ANY TWO POINTS”

ConsoleWriteline “Enter the value of x for 1st 2D constructor”

Console.ReadLine();

Console Writeline “Enter the value of y for 1st 2D constructor”

Console.ReadLine();

ConsoleWriteline “Enter the value of z for 1st 3D constructor”

Console.ReadLine();

ConsolePrintLine“Enter the value of x for 2 nd 2D constructor”

Console.ReadLine();

ConsoleWriteline “Enter the value of y for 2 nd 2D constructor”

Console.ReadLine();

ConsolePrintLine “Enter the value of z for 2nd 3D constructor”

Console.ReadLine();

ConsolePrintLine “The graphical representation of point in graph”

ConsolePrintLine “2D points distance between A and B is = output answer”

ConsolePrintLine “3D points distance between A and B is = output answer”

ConsolePrintLine “Enter any key to exit”

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

ConsoleWriteline “Enter the value of x for


ConsolePrintLine “The graphical
1st 2D constructor”
representation of point in graph”
ConsoleWriteline “Enter the value of x for
2nd 2D constructor”
Console.ReadLine();

ConsolePrintLine “2D points distance


between A and B is = output answer”
Console.ReadLine();
ConsoleWriteline “Enter the value of y for
1st 2D constructor”

ConsolePrintLine “3D points distance


between A and B is = output answer”
ConsoleWriteline “Enter the value of y for
2nd 2D constructor”
Console.ReadLine();

ConsolePrintLine “press the Enter key to exit”


Console.ReadLine();
A

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

4.1 TEST PLAN


Test Purpose of Test Test Data Expected Result Pass/F Date
No. ail

1 Test welcome “WELCOME TO Program should be able to display Pass 19/08/2010


message 2D AND 3D POINT “WELCOME TO 2D AND 3D
CALCULATOR” POINT CALCULATOR”
“THIS PROGRAM “THIS PROGRAM
CALCULATES” CALCULATES”
"THE DISTANCE "THE DISTANCE IN 2D AND
IN 2D AND 3D 3D BETWEEN ANY TWO
BETWEEN ANY POINTS”
TWO POINTS”
2 Test input for 1st x “3” Program should be able to receive Pass 19/08/2010
co-ordinate integer input “3”

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

Test No. Purpose of Test Actual Result Result Comment Date


(see Test Pass/F
plan) ail
1 Test welcome “WELCOME TO Pass Program display the welcome 19/08/2010
message 2D AND 3D message to user.
POINT
CALCULATOR”
“THIS PROGRAM
CALCULATES”
"THE DISTANCE
IN 2D AND 3D
BETWEEN ANY
TWO POINTS”
2 Test for 1st x co- “3” Pass Program was able to receive 19/08/2010
ordinate integer as put for 1st x co-
ordinate
3 Test for 1st y co- “5” Pass Program was able to receive 19/08/2010
ordinate integer as put for 1st y co-
ordinate
4 Test for 1st z co- “4” Pass Program was able to receive 19/08/2010
ordinate integer as put for 1st z co-
ordinate
5 Test for 2nd x co- “5” Pass Program was able to receive 19/08/2010
ordinate integer as put for 2nd x co-
ordinate
6 Test for 2nd y co- “7” Pass Program was able to receive 19/08/2010
ordinate integer as put for 2nd y co-
ordinate
7 Test for 2nd z co- “6” Pass Program was able to receive 19/08/2010
ordinate integer as put for 2nd z co-
ordinate
8 Test for 2DPoints, “14.212670403551 Pass Program was able to 19/08/2010
Distance between A 9” calculate and display result
and B
9 Test for 3DPoints, “3.4641016151377 Pass Program was able to display 19/08/2010
Distance between A 5” 3Dpoints result
and B
10 Test for display of B Pass Program was able to display 19/08/2010
2dpoints and the position of the A and B
3dpoints on graph A co-ordinates on the graph.

98
4.3 PRINT SCREEN

Fig1: The welcome 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]

Available at: http://www.brothersoft.com/visual-c-2008-express-edition-67705.html

[accessed 12th august, 2010]

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.

• To access or use program run the program.

• 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)

• Graph will be displayed

• 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

• Press the “Enter” key to exit from the program.

103

You might also like