[英]Can't get the right answer while finding angle in C#
我正在做 coursera 上的 Unity 簡介課程。 我已經達到了第一個編程任務,即找到兩點的斜邊和角度。 我幾乎把代碼弄對了,但有幾個答案是對的,有幾個是錯的。 我不知道出了什么問題。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProgrammingAssignment1
{
// IMPORTANT: Only add code in the section
// indicated below. The code I've provided
// makes your solution work with the
// automated grader on Coursera
/// <summary>
/// Programming Assignment 1
/// </summary>
class Program
{
// x and y coordinates for points
static float point1X;
static float point1Y;
static float point2X;
static float point2Y;
/// <summary>
/// Calculates angle and distance between two points
/// </summary>
/// <param name="args">command-line args</param>
static void Main(string[] args)
{
// loop while there's more input
string input = Console.ReadLine();
while (input[0] != 'q')
{
// extract point coordinates from string
GetInputValuesFromString(input);
// Add your code between this comment
// and the comment below. You can of
// course add more space between the
// comments as needed
float deltaX = point1X - point2X;
float deltaY = point1Y - point2Y;
float hypotenuse = (float)Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2));
float angleInRadians = (float)Math.Atan2(deltaY, deltaX);
float angleInDegrees = angleInRadians * 180 / (float)Math.PI - 180;
Console.WriteLine(hypotenuse + " " + angleInDegrees);
// Don't add or modify any code below
// this comment
input = Console.ReadLine();
}
}
/// <summary>
/// Extracts point coordinates from the given input string
/// </summary>
/// <param name="input">input string</param>
static void GetInputValuesFromString(string input)
{
// extract point 1 x
int spaceIndex = input.IndexOf(' ');
point1X = float.Parse(input.Substring(0, spaceIndex));
// move along string and extract point 1 y
input = input.Substring(spaceIndex + 1);
spaceIndex = input.IndexOf(' ');
point1Y = float.Parse(input.Substring(0, spaceIndex));
// move along string and extract point 2 x
input = input.Substring(spaceIndex + 1);
spaceIndex = input.IndexOf(' ');
point2X = float.Parse(input.Substring(0, spaceIndex));
// point 2 y is the rest of the string
input = input.Substring(spaceIndex + 1);
point2Y = float.Parse(input);
#region Unfortunately, Mono doesn't have a Split method!
//string[] values = input.Split(' ');
//point1X = float.Parse(values[0]);
//point1Y = float.Parse(values[1]);
//point2X = float.Parse(values[2]);
//point2Y = float.Parse(values[3]);
#endregion
}
}
}
您可以看到我在添加您的代碼注釋之后添加的代碼,因此我成功地正確找到了斜邊,但角度是我出錯的地方!
float angleInRadians = (float)Math.Atan2(deltaY, deltaX);
float angleInDegrees = angleInRadians * 180 / (float)Math.PI - 180;
這是找到角度的兩條線。 因此,當我輸入點 5 5 4 4 時,它在第二行給了我正確的角度 Math.PI - 180,但同時當我輸入 2 2 4 4 時,它給了我錯誤的答案。
所以我玩了一會兒,添加了 Math.PI + 180,所以這次我得到了 2 2 4 4 的正確角度和 5 5 4 4 的錯誤答案。我只是不知道我哪里出錯了!
一些幫助將不勝感激!
嘗試這個:
//radians into degrees
var angle = (radians * (180 / Math.PI) + 360) % 360;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.