c# Program to Calculate the Distance Between Two Points in 2d and 3d

24
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

Transcript of c# Program to Calculate the Distance Between Two Points in 2d and 3d

Page 1: c# Program to Calculate the Distance Between Two Points in 2d and 3d

Table of Contents1.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

Page 2: c# Program to Calculate the Distance Between Two Points in 2d and 3d

1.0 INTRODUCTIONTechnology 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

Page 3: c# Program to Calculate the Distance Between Two Points in 2d and 3d

2.0 SOURCE CODE

2.1 CLASS COORDINATETESTusing 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");

3

Page 4: c# Program to Calculate the Distance Between Two Points in 2d and 3d

object2.drawGraph(object3, object4);

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 CLASS2DPOINTusing 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

Page 5: c# Program to Calculate the Distance Between Two Points in 2d and 3d

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 CLASS3DPOINTusing 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

Page 6: c# Program to Calculate the Distance Between Two Points in 2d and 3d

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

Page 7: c# Program to Calculate the Distance Between Two Points in 2d and 3d

} }}3.0 DESIGN

3.1 UMLThe UML of this program defines the distance between two points. The entities of the UML are the classes.

7

inheritance

Class3DPointClass2DPoint

#x: int #y: int

+ Class2DPoint() +Class2DPoint(): int+ showDistance(Class2DPoint secondPoint): double + drawGraph(Class2DPoint obj, Class2DPoint obj1): void

-z (int)

+ Class2DPoint() +Class2DPoint(intx, int y): int+ showDistance(Class3DPoint secondPoint): double+ drawGraph(Class3DPoint obj, Class3DPoint obj1): void

Class2DPoint

Class3DPoint

+ Class2DPoint() +Class2DPoint(int x, int y)+ showDistance(Class2DPoint secondPoint) + drawGraph(Class2DPoint obj, Class2DPoint obj1)

-z (int)

#x (int) # y(int)

Page 8: c# Program to Calculate the Distance Between Two Points in 2d and 3d

3.2 DATA DICTIONARY - = {private}# = {protected}+ = {public}() = {constructor}Int = {32bit numeric}String = {alphabet + numeric + (ASCII code)}x = {value of 1st Class2DPoint constructor + value of 2nd Class2DPoint constructor}y = {value of 1st Class3DPoint constructor + value of 2nd 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

Page 9: c# Program to Calculate the Distance Between Two Points in 2d and 3d

3.3 PSEUDO CODEStart

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 2nd 2D constructor”

Console.ReadLine();

ConsoleWriteline “Enter the value of y for 2nd 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

Page 10: c# Program to Calculate the Distance Between Two Points in 2d and 3d

3.4 FLOWCHART

10

A

Console.ReadLine();

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

Console.ReadLine();

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

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

“THIS PROGRAM CALCULATES”

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

start

Page 11: c# Program to Calculate the Distance Between Two Points in 2d and 3d

11

Page 12: c# Program to Calculate the Distance Between Two Points in 2d and 3d

12

Page 13: c# Program to Calculate the Distance Between Two Points in 2d and 3d

4.0 TESTING

4.1 TEST PLANTest No.

Purpose of Test Test Data Expected Result Pass/Fail

Date

1 Test welcome message

“WELCOME TO 2D AND 3D POINT

CALCULATOR”“THIS PROGRAM CALCULATES”

"THE DISTANCE IN 2D AND 3D

BETWEEN ANY TWO POINTS”

Program should be able to display “WELCOME TO 2D AND 3D

POINT CALCULATOR”“THIS PROGRAM CALCULATES”

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

POINTS”

Pass 19/08/2010

2 Test input for 1st x co-ordinate

“3” Program should be able to receive integer input “3”

Pass 19/08/2010

3 Test input for 1st y co-ordinate

“5” Program should be able to receive integer input “5”

pass 19/08/2010

4 Test input for 1st z co-ordinate

“4” Program should be able to receive integer input “4”

pass 19/08/2010

5 Test input for 2nd x co-ordinate

“5” Program should be able to receive integer input “5”

pass 19/08/2010

6 Test input for 2nd y co-ordinate

“7” Program should be able to receive integer input “7”

Pass 19/08/2010

7 Test input for 2nd z co-ordinate

“6” Program should be able to receive integer input “6”

pass 19/08/2010

13

Page 14: c# Program to Calculate the Distance Between Two Points in 2d and 3d

8 Test for 2DPoints, Distance between A and B

“ calculated 2Dpoints distance answer”

Program should be able to display the answer of the calculated distance between A and B

pass 19/08/2010

9 Test for 3DPoints, Distance between A and B

“ calculated distance 3Dpoints answer”

Program should be to calculate and display the answer of the both 2Dpoints and 3Dpointd.

Pass 19/082010

10 Test for display of 2dpoints and 3dpoints on graph.

“2Dpoints and 3Dpoints answer on graph”

Program should be able to display the result of 2Dpoints and 3Dpoints on the graph.

pass 19/08/2010

4.2 TEST LOG

Test No.(see Test plan)

Purpose of Test Actual Result ResultPass/Fail

Comment Date

1 Test welcome message

“WELCOME TO 2D AND 3D

POINT CALCULATOR”

“THIS PROGRAM CALCULATES”

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

Pass Program display the welcome message to user.

19/08/2010

2 Test for 1st x co-ordinate

“3” Pass Program was able to receive integer as put for 1st x co-ordinate

19/08/2010

3 Test for 1st y co-ordinate

“5” Pass Program was able to receive integer as put for 1st y co-ordinate

19/08/2010

4 Test for 1st z co-ordinate

“4” Pass Program was able to receive integer as put for 1st z co-ordinate

19/08/2010

5 Test for 2nd x co-ordinate

“5” Pass Program was able to receive integer as put for 2nd x co-ordinate

19/08/2010

6 Test for 2nd y co-ordinate

“7” Pass Program was able to receive integer as put for 2nd y co-ordinate

19/08/2010

14

Page 15: c# Program to Calculate the Distance Between Two Points in 2d and 3d

7 Test for 2nd z co-ordinate

“6” Pass Program was able to receive integer as put for 2nd z co-ordinate

19/08/2010

8 Test for 2DPoints, Distance between A and B

“14.2126704035519”

Pass Program was able to calculate and display result

19/08/2010

9 Test for 3DPoints, Distance between A and B

“3.46410161513775”

Pass Program was able to display 3Dpoints result

19/08/2010

10 Test for display of 2dpoints and 3dpoints on graph

B

A

Pass Program was able to display the position of the A and B co-ordinates on the graph.

19/08/2010

4.3 PRINT SCREEN

Fig1: The welcome screen

15

Page 16: c# Program to Calculate the Distance Between Two Points in 2d and 3d

Fig2: The input of the x, y and z coordinates for the 2D and 3D points

16

Page 17: c# Program to Calculate the Distance Between Two Points in 2d and 3d

Fig3: The graph for the 2D and 3D points are plotted and the distance between the points are calculated

5.0 CONCLUSIONThis 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.

17

Page 18: c# Program to Calculate the Distance Between Two Points in 2d and 3d

18

Page 19: c# Program to Calculate the Distance Between Two Points in 2d and 3d

6.0 REFERENCEMicrosoft 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]

19

Page 20: c# Program to Calculate the Distance Between Two Points in 2d and 3d

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.

20