2010年4月10日 星期六

作業八:機器人駕駛(解答)

宣告一個迴圈,同時做讀取機器人走的方位與步數以及將字串拆解成陣列的動作,C#語言程式碼如下

for (i = 0; i < cases; i++)
{
int m = int.Parse(Console.ReadLine());
for (j = 0; j < m; j++)
{
string instruct = Console.ReadLine();
string[] value = instruct.Split(' ');
qus.Blue[i].MoveTo(value[0], value[1]);
qus.Red[i].MoveTo(value[2], value[3]);
}
}

計算方位與距離

double pointView = Math.Atan((qus.Blue[i].y - qus.Red[i].y) /
(qus.Blue[i].x - qus.Red[i].x)) * 180 / Math.PI;
double distance = Math.Sqrt((qus.Blue[i].x - qus.Red[i].x) * (qus.Blue[i].x - qus.Red[i].x) +
(qus.Blue[i].y - qus.Red[i].y) * (qus.Blue[i].y - qus.Red[i].y));

最後判斷以一點為中心另一點是在哪個象限再決定是要加90&270或者減90&270

if ((qus.Red[i].x - qus.Blue[i].x) >= 0 && (qus.Red[i].y - qus.Blue[i].y) >= 0)
{
Console.Write("Blue: {0:f3}, {1} Red: {2:f3}, {3}", distance, Math.Round(90 - pointView), distance, Math.Round(270 - pointView));
}
if ((qus.Red[i].x - qus.Blue[i].x) >= 0 && (qus.Red[i].y - qus.Blue[i].y) <= 0) { Console.Write("Blue: {0:f3}, {1} Red: {2:f3}, {3}", distance, Math.Round(90 - pointView), distance, Math.Round(270 - pointView)); } if ((qus.Red[i].x - qus.Blue[i].x) <= 0 && (qus.Red[i].y - qus.Blue[i].y) >= 0)
{
Console.Write("Blue: {0:f3}, {1} Red: {2:f3}, {3}", distance, Math.Round(270 - pointView), distance, Math.Round(90 - pointView));
}
if ((qus.Red[i].x - qus.Blue[i].x) <= 0 && (qus.Red[i].y - qus.Blue[i].y) <= 0) { Console.Write("Blue: {0:f3}, {1} Red: {2:f3}, {3}", distance, Math.Round(270 - pointView), distance, Math.Round(90 - pointView)); }
作業九題目
回C#語言教學目錄
回首頁

沒有留言: