1

quite new to C#. I have the following code that calculates the distance and angle between two points. However, it won't display the decimal points (needs to be to three decimal places. I thought that float data type could handle decimal numbers?

e.g. point 1 x = 2, point 1 y = 2, point 2 x = 1, point 2 y = 1.

Distance gets calculated as 1 and angle gets calculated as -1. Distance should be 1.414 & angle should be -135.000 degrees, so its like its rounded them up / down if that makes sense...

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

namespace AngleDistanceCalc
{
    class Program
    {
        static void Main(string[] args)
        {
            // print welcome message
            Console.WriteLine("Welcome. This application will calculate the distance between two points and display the angle.");

            Console.WriteLine("Please enter point 1 X value:");
            float point1X = float.Parse(Console.ReadLine());

            Console.WriteLine("Please enter point 1 Y value:");
            float point1Y = float.Parse(Console.ReadLine());

            Console.WriteLine("Please enter point 2 X value:");
            float point2X = float.Parse(Console.ReadLine());

            Console.WriteLine("Please enter point 2 y value:");
            float point2Y = float.Parse(Console.ReadLine());

            float deltaX = point2X - point1X;
            float deltaY = point2Y - point2X;

            double distance = Math.Sqrt(deltaX * deltaX + deltaY * deltaY);

            Console.WriteLine("The distance between the points is: {0}", distance);

            Console.WriteLine("The angle between the points is: {0}",  deltaX);
        }
    }
}
Douglas
  • 53,759
  • 13
  • 140
  • 188
Sam
  • 325
  • 3
  • 15
  • 1
    `it won't display the decimal points` What is the output you get, what do you expect? – Eser Apr 03 '16 at 21:09
  • point 1 x = 2, point 1 y = 2, point 2 x = 1, point 2 y = 1. Distance gets calculated as 1 and angle gets calculated as -1. Distance should be 1.414 & angle should be -135.000 degrees, so its like its rounded them up / down if that makes sense... – Sam Apr 03 '16 at 21:13
  • No, It doesn't make sense. My math is enough to do that, My question was: `What is the output you get, what do you expect?` Show concrete samples... – Eser Apr 03 '16 at 21:16
  • You have `point2Y - point2X`... Using a debugger would've shown that you get a wrong number and you could've checked why. – Sami Kuhmonen Apr 03 '16 at 21:16

1 Answers1

2
float deltaY = point2Y - point2X;

You have a bug in the above line. You need to compute:

float deltaY = point2Y - point1Y;

Also, you need to introduce logic for computing the angle. The formula is discussed under this answer:

var angle = Math.Atan2(deltaY, deltaX) * 180 / Math.PI;
Console.WriteLine("The angle between the points is: {0}", angle);
Community
  • 1
  • 1
Douglas
  • 53,759
  • 13
  • 140
  • 188
  • Ahhh that makes sense. Solved it! Thank you! How can I display the output to three decimal places? – Sam Apr 03 '16 at 21:32
  • @SamFarr: Use the [Numeric ("N") Format Specifier](https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#Anchor_6): `Console.WriteLine("The angle between the points is: {0:N3}", angle);` – Douglas Apr 03 '16 at 21:34
  • @LutzL: You're right; thanks for pointing that out. I've corrected the answer. – Douglas Apr 04 '16 at 11:13
  • Got it, thanks again :) – Sam Apr 05 '16 at 12:19