Replies: 19 comments 11 replies
-
JIA(程式新手) 我的答案public float magnitude
{
get => (float)Math.Sqrt(x * x + y * y);
}
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
public void Normalize()
{
float length = magnitude;
if (length > 0)
{
x /= length;
y /= length;
}
}
public float Magnitude()
{
return magnitude;
}
public void Negative()
{
x *= -1;
y *= -1;
}
public void Add(Vector2 v)
{
x += v.x;
y += v.y;
}
public void Subtract(Vector2 v)
{
x -= v.x;
y -= v.y;
}
public static Vector2 Normalize(Vector2 v)
{
v.Normalize();
return v;
}
public static float Distance(Vector2 from, Vector2 to)
{
return new Vector2(to.x - from.x, to.y - from.y).magnitude;
} 看到題目直接滿頭問號XD |
Beta Was this translation helpful? Give feedback.
-
小4 ( 程式 ) public struct Vector2
{
private const float kEpsilon = 1E-05f;
public static readonly Vector2 Zero = new Vector2(0, 0);
public static readonly Vector2 One = new Vector2(1, 1);
public static readonly Vector2 Left = new Vector2(-1, 0);
public static readonly Vector2 Right = new Vector2(1, 0);
public static readonly Vector2 Up = new Vector2(0, 1);
public static readonly Vector2 Down = new Vector2(0, -1);
public float x;
public float y;
public float magnitude { get; }
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
magnitude = (float)Math.Sqrt(x * x + y * y);
}
public void Normalize()
{
float len = (float)Math.Sqrt(x * x + y * y);
if (len > kEpsilon)
{
x /= len;
y /= len;
}
else
{
x = 0;
y = 0;
}
}
public float Magnitude()
{
return (float)Math.Sqrt(x * x + y * y);
}
public void Negative()
{
x *= -1;
y *= -1;
}
public void Add(Vector2 v)
{
x += v.x;
y += v.y;
}
public void Subtract(Vector2 v)
{
x -= v.x;
y -= v.y;
}
public static Vector2 Normalize(Vector2 v)
{
float len = (float)Math.Sqrt(v.x * v.x + v.y * v.y);
if (len > kEpsilon)
{
v.x /= len;
v.y /= len;
}
else
{
v.x = 0;
v.y = 0;
}
return v;
}
public static float Distance(Vector2 from, Vector2 to)
{
float newX = from.x - to.x;
float newY = from.y - to.y;
return new Vector2(newX, newY).magnitude;
}
} 扯 Distance竟然被我矇對 |
Beta Was this translation helpful? Give feedback.
-
老蕭OLDShaw(程式) 我的答案public class Question1
{
public struct Vector2
{
/// <summary>
/// The tolerance of the float value
/// </summary>
private const float kEpsilon = 1E-05f;
#region -- Constant Vectors --
public static readonly Vector2 Zero = new Vector2(0, 0);
public static readonly Vector2 One = new Vector2(1, 1);
public static readonly Vector2 Left = new Vector2(-1, 0);
public static readonly Vector2 Right = new Vector2(1, 0);
public static readonly Vector2 Up = new Vector2(0, 1);
public static readonly Vector2 Down = new Vector2(0, -1);
#endregion
/// <summary>
/// x-axis
/// </summary>
public float x;
/// <summary>
/// y-axis
/// </summary>
public float y;
/// <summary>
/// Length of vector
/// </summary>
public float magnitude { get { return Magnitude(); }}
#region -- Constructor --
/// <summary>
/// Constructor for newing vector
/// </summary>
/// <param name="x">init x-axis</param>
/// <param name="y">init y-axis</param>
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
#endregion
/// <summary>
/// Normalize current vector
/// </summary>
public void Normalize()
{
float currentMagnitude = magnitude;
x /= currentMagnitude;
y /= currentMagnitude;
}
/// <summary>
/// Calculate current length of vector
/// </summary>
/// <returns>length of vector</returns>
public float Magnitude()
{
return (float)Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
}
/// <summary>
/// Revert current vector
/// </summary>
public void Negative()
{
x = -x;
y = -y;
}
/// <summary>
/// Add other vector
/// </summary>
public void Add(Vector2 v)
{
x += v.x;
y += v.y;
}
/// <summary>
/// Subtract other vector
/// </summary>
public void Subtract(Vector2 v)
{
x -= v.x;
y -= v.y;
}
#region -- Static Methods --
/// <summary>
/// Normalize specific vector
/// </summary>
/// <param name="v">the specific vector to normalize</param>
/// <returns>Normalized vector</returns>
public static Vector2 Normalize(Vector2 v)
{
float targetMagnitude = (float)Math.Sqrt(Math.Pow(v.x, 2) + Math.Pow(v.y, 2));
v.x /= targetMagnitude;
v.y /= targetMagnitude;
return v;
}
/// <summary>
/// Calculate distence between vectors
/// </summary>
/// <param name="from">the start vector</param>
/// <param name="to">the end vector</param>
/// <returns>Distance between from and to vectors</returns>
public static float Distance(Vector2 from, Vector2 to)
{
return (float)Math.Sqrt(Math.Pow(from.x - to.x, 2) + Math.Pow(from.y - to.y, 2));
}
#endregion
}
} 不確定需不需要設一個Bool來去防止Magnitude或是Normalize重複進行無謂的計算。 |
Beta Was this translation helpful? Give feedback.
-
肉鬆(程式 / 音樂) 我的答案 public struct Vector2
{
public float magnitude
{
get
{
return (float)Math.Sqrt((x * x) + (y * y));
}
}
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
public void Normalize()
{
float len = magnitude;
if (len < kEpsilon)
{
x = 0;
y = 0;
}
else if (len >= kEpsilon)
{
x /= len;
y /= len;
}
}
public float Magnitude()
{
return (float)Math.Sqrt((x * x) + (y * y));
}
public void Negative()
{
x *= -1;
y *= -1;
}
public void Add(Vector2 v)
{
this.x += v.x;
this.y += v.y;
}
public void Subtract(Vector2 v)
{
this.x -= v.x;
this.y -= v.y;
}
public static Vector2 Normalize(Vector2 v)
{
v.Normalize();
return v;
}
public static float Distance(Vector2 from, Vector2 to)
{
float newX = from.x - to.x;
float newY = from.y - to.y;
return (float)Math.Sqrt((newX * newX) + (newY * newY));
}
} 課程回饋這次突然跳躍到數學了 一開始真的不知道從哪裡下手 Orzz |
Beta Was this translation helpful? Give feedback.
-
Syuan(新人/企劃/程式/美術) 我的答案 public struct Vector2
{
private const float kEpsilon = 1E-05f;
#region -- Constant Vectors --
public static readonly Vector2 Zero = new Vector2(0, 0);
public static readonly Vector2 One = new Vector2(1, 1);
public static readonly Vector2 Left = new Vector2(-1, 0);
public static readonly Vector2 Right = new Vector2(1, 0);
public static readonly Vector2 Up = new Vector2(0, 1);
public static readonly Vector2 Down = new Vector2(0, -1);
#endregion
public float x;
public float y;
public float magnitude { get { return Magnitude(); } }
#region -- Constructor --
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
#endregion
public void Normalize()
{
float len = magnitude;
if (len > kEpsilon)
{ x /= len; y /= len; }
else
{ x = 0; y = 0; }
}
public float Magnitude()
{
return (float)Math.Sqrt(x * x + y * y);
}
public void Negative()
{
x = -x;y = -y;
}
public void Add(Vector2 v)
{
x += v.x;y += v.y;
}
public void Subtract(Vector2 v)
{
x -= v.x; y -= v.y;
}
#region -- Static Methods --
public static Vector2 Normalize(Vector2 v)
{
v.Normalize();
return v;
}
public static float Distance(Vector2 from, Vector2 to)
{
float X = from.x - to.x;float Y = from.y - to.y;
return (float)Math.Sqrt(X*X+Y*Y);
}
#endregion
} |
Beta Was this translation helpful? Give feedback.
-
吉米(不知道要學什麼但是喜歡創作的新人) 答案在這 /// <summary>
/// The tolerance of the float value
/// </summary>
private const float kEpsilon = 1E-05f;
#region -- Constant Vectors --
public static readonly Vector2 Zero = new Vector2(0, 0);
public static readonly Vector2 One = new Vector2(1, 1);
public static readonly Vector2 Left = new Vector2(-1, 0);
public static readonly Vector2 Right = new Vector2(1, 0);
public static readonly Vector2 Up = new Vector2(0, 1);
public static readonly Vector2 Down = new Vector2(0, -1);
#endregion
/// <summary>
/// x-axis
/// </summary>
public float x;
/// <summary>
/// y-axis
/// </summary>
public float y;
/// <summary>
/// Length of vector
/// </summary>
public float magnitude { get => MathF.Sqrt(x * x + y * y); }
#region -- Constructor --
/// <summary>
/// Constructor for newing vector
/// </summary>
/// <param name="x">init x-axis</param>
/// <param name="y">init y-axis</param>
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
#endregion
/// <summary>
/// Normalize current vector
/// </summary>
public void Normalize()
{
float len = (float)MathF.Sqrt(x*x + y*y);
if(len > kEpsilon)
{
x /= len;
y /= len;
}
else
{
x = 0;
y = 0;
}
}
/// <summary>
/// Calculate current length of vector
/// </summary>
/// <returns>length of vector</returns>
public float Magnitude()
{
return MathF.Sqrt(x* x + y * y);
}
/// <summary>
/// Revert current vector
/// </summary>
public void Negative()
{
x *= -1;
y *= -1;
}
/// <summary>
/// Add other vector
/// </summary>
public void Add(Vector2 v)
{
x += v.x;
y += v.y;
}
/// <summary>
/// Subtract other vector
/// </summary>
public void Subtract(Vector2 v)
{
x -= v.x;
y -= v.y;
}
#region -- Static Methods --
/// <summary>
/// Normalize specific vector
/// </summary>
/// <param name="v">the specific vector to normalize</param>
/// <returns>Normalized vector</returns>
public static Vector2 Normalize(Vector2 v)
{
v.Normalize();
return v;
}
/// <summary>
/// Calculate distence between vectors
/// </summary>
/// <param name="from">the start vector</param>
/// <param name="to">the end vector</param>
/// <returns>Distance between from and to vectors</returns>
public static float Distance(Vector2 from, Vector2 to)
{
float DistanceX = to.x - from.x;
float DistanceY = to.y - from.y;
return (float)Math.Sqrt(DistanceX * DistanceX + DistanceY * DistanceY);
}
#endregion 哇勒,原本想說我應該都會,都有看過有概念,結果看到題目直接矇XD |
Beta Was this translation helpful? Give feedback.
-
Cliff Lee CL (程式、專案管理) 我的答案public struct Vector2
{
/// <summary>
/// The tolerance of the float value
/// </summary>
private const float kEpsilon = 1E-05f;
#region -- Constant Vectors --
public static readonly Vector2 Zero = new Vector2(0, 0);
public static readonly Vector2 One = new Vector2(1, 1);
public static readonly Vector2 Left = new Vector2(-1, 0);
public static readonly Vector2 Right = new Vector2(1, 0);
public static readonly Vector2 Up = new Vector2(0, 1);
public static readonly Vector2 Down = new Vector2(0, -1);
#endregion
/// <summary>
/// x-axis
/// </summary>
public float x;
/// <summary>
/// y-axis
/// </summary>
public float y;
/// <summary>
/// Length of vector
/// </summary>
public float magnitude { get; }
#region -- Constructor --
/// <summary>
/// Constructor for newing vector
/// </summary>
/// <param name="x">init x-axis</param>
/// <param name="y">init y-axis</param>
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
magnitude = MathF.Sqrt(x * x + y * y);
}
#endregion
/// <summary>
/// Normalize current vector
/// </summary>
public void Normalize()
{
x /= magnitude;
y /= magnitude;
}
/// <summary>
/// Calculate current length of vector
/// </summary>
/// <returns>length of vector</returns>
public float Magnitude()
{
return magnitude;
}
/// <summary>
/// Revert current vector
/// </summary>
public void Negative()
{
x = -x;
y = -y;
}
/// <summary>
/// Add other vector
/// </summary>
public void Add(Vector2 v)
{
x += v.x;
y += v.y;
}
/// <summary>
/// Subtract other vector
/// </summary>
public void Subtract(Vector2 v)
{
x -= v.x;
y -= v.y;
}
#region -- Static Methods --
/// <summary>
/// Normalize specific vector
/// </summary>
/// <param name="v">the specific vector to normalize</param>
/// <returns>Normalized vector</returns>
public static Vector2 Normalize(Vector2 v)
{
v.Normalize();
return v;
}
/// <summary>
/// Calculate distence between vectors
/// </summary>
/// <param name="from">the start vector</param>
/// <param name="to">the end vector</param>
/// <returns>Distance between from and to vectors</returns>
public static float Distance(Vector2 from, Vector2 to)
{
return (float)Math.Sqrt(Math.Pow(to.x - from.x, 2) + Math.Pow(to.y - from.y, 2));
}
#endregion
} |
Beta Was this translation helpful? Give feedback.
-
ChiaBurn(程式 | 企劃) 作業程式碼{
public struct Vector2
{
...
public float magnitude
{
get
{
return (float)Math.Sqrt(x * x + y * y);
}
}
#region -- Constructor --
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
#endregion
public void Normalize()
{
var len = this.magnitude;
if (len > kEpsilon)
{
x /= len;
y /= len;
}
else
{
x = 0;
y = 0;
}
}
public float Magnitude()
{
return (float)Math.Sqrt(x * x + y * y);
}
public void Negative()
{
this.x *= -1;
this.y *= -1;
}
public void Add(Vector2 v)
{
this.x += v.x;
this.y += v.y;
}
public void Subtract(Vector2 v)
{
this.x -= v.x;
this.y -= v.y;
}
#region -- Static Methods --
public static Vector2 Normalize(Vector2 v)
{
var len = v.magnitude;
if (len > kEpsilon)
{
return new Vector2(v.x / len, v.y / len);
}
else
{
return new Vector2(0, 0);
}
}
public static float Distance(Vector2 from, Vector2 to)
{
var distanceX = to.x - from.x;
var distanceY = to.y - from.y;
return (float)Math.Sqrt(distanceX * distanceX + distanceY * distanceY);
}
#endregion
}
} 想法與反饋
public static Vector2 Normalize(Vector2 v)
{
v.Normalize(); // 直接叫用前面寫好的方法
return v;
} 雖然能通過測試,但想想覺得怪,因為作為使用者的話,我看到方法有return Vector2就會傾向認定這個方法只會幫我算出一個新的值、不會動到我原本的input。
|
Beta Was this translation helpful? Give feedback.
-
我的答案public class Question1 { public struct Vector2 { ////// The tolerance of the float value ///private const float kEpsilon = 1E-05f;
我的感想太多數學了 太多英文了 幹 |
Beta Was this translation helpful? Give feedback.
-
罐頭(程式) 我的答案ヾ(•ω•`)ousing System;
namespace PG0003.Questions
{
public class Question1
{
public struct Vector2
{
/// <summary>
/// The tolerance of the float value
/// </summary>
private const float kEpsilon = 1E-05f;
#region -- Constant Vectors --
public static readonly Vector2 Zero = new Vector2(0, 0);
public static readonly Vector2 One = new Vector2(1, 1);
public static readonly Vector2 Left = new Vector2(-1, 0);
public static readonly Vector2 Right = new Vector2(1, 0);
public static readonly Vector2 Up = new Vector2(0, 1);
public static readonly Vector2 Down = new Vector2(0, -1);
#endregion
/// <summary>
/// x-axis
/// </summary>
public float x;
/// <summary>
/// y-axis
/// </summary>
public float y;
/// <summary>
/// Length of vector
/// </summary>
public float magnitude => Magnitude();
#region -- Constructor --
/// <summary>
/// Constructor for newing vector
/// </summary>
/// <param name="x">init x-axis</param>
/// <param name="y">init y-axis</param>
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
#endregion
/// <summary>
/// Normalize current vector
/// </summary>
public void Normalize()
{
float len = magnitude;
if (len < kEpsilon)
{
x = 0;
y = 0;
}
else
{
x /= len;
y /= len;
}
}
/// <summary>
/// Calculate current length of vector
/// </summary>
/// <returns>length of vector</returns>
public float Magnitude()
{
return (float)Math.Sqrt(x * x + y * y);
}
/// <summary>
/// Revert current vector
/// </summary>
public void Negative()
{
x = -x;
y = -y;
}
/// <summary>
/// Add other vector
/// </summary>
public void Add(Vector2 v)
{
x += v.x;
y += v.y;
}
/// <summary>
/// Subtract other vector
/// </summary>
public void Subtract(Vector2 v)
{
x -= v.x;
y -= v.y;
}
#region -- Static Methods --
/// <summary>
/// Normalize specific vector
/// </summary>
/// <param name="v">the specific vector to normalize</param>
/// <returns>Normalized vector</returns>
public static Vector2 Normalize(Vector2 v)
{
v.Normalize();
return v;
}
/// <summary>
/// Calculate distence between vectors
/// </summary>
/// <param name="from">the start vector</param>
/// <param name="to">the end vector</param>
/// <returns>Distance between from and to vectors</returns>
public static float Distance(Vector2 from, Vector2 to)
{
float xDiff = to.x - from.x;
float yDiff = to.y - from.y;
return (float)Math.Sqrt((xDiff * xDiff) + (yDiff * yDiff));
}
#endregion
}
}
} 抱歉,這次的作業比較晚交~突然就開始進入到偶的未知領域了,花了不少功夫理解,希望最終的作業有達到老師的期望。有勞老師過目了,感謝(;´д`)ゞ |
Beta Was this translation helpful? Give feedback.
-
Snoweve (程式) 我的答案using System;
namespace PG0003.Questions
{
public class Question1
{
public struct Vector2
{
/// <summary>
/// The tolerance of the float value
/// </summary>
private const float kEpsilon = 1E-05f;
#region -- Constant Vectors --
public static readonly Vector2 Zero = new Vector2(0, 0);
public static readonly Vector2 One = new Vector2(1, 1);
public static readonly Vector2 Left = new Vector2(-1, 0);
public static readonly Vector2 Right = new Vector2(1, 0);
public static readonly Vector2 Up = new Vector2(0, 1);
public static readonly Vector2 Down = new Vector2(0, -1);
#endregion
/// <summary>
/// x-axis
/// </summary>
public float x;
/// <summary>
/// y-axis
/// </summary>
public float y;
/// <summary>
/// Length of vector
/// </summary>
public float magnitude
{
get => Magnitude();
}
#region -- Constructor --
/// <summary>
/// Constructor for newing vector
/// </summary>
/// <param name="x">init x-axis</param>
/// <param name="y">init y-axis</param>
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
#endregion
/// <summary>
/// Normalize current vector
/// </summary>
public void Normalize()
{
float lenMagnitude = magnitude;
if (lenMagnitude < kEpsilon)
{
x = 0;
y = 0;
}
else if (lenMagnitude >= kEpsilon)
{
x /= lenMagnitude;
y /= lenMagnitude;
}
}
/// <summary>
/// Calculate current length of vector
/// </summary>
/// <returns>length of vector</returns>
public float Magnitude()
{
return (float)Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
}
/// <summary>
/// Revert current vector
/// </summary>
public void Negative()
{
x = -x;
y = -y;
}
/// <summary>
/// Add other vector
/// </summary>
public void Add(Vector2 v)
{
x += v.x;
y += v.y;
}
/// <summary>
/// Subtract other vector
/// </summary>
public void Subtract(Vector2 v)
{
x -= v.x;
y -= v.y;
}
#region -- Static Methods --
/// <summary>
/// Normalize specific vector
/// </summary>
/// <param name="v">the specific vector to normalize</param>
/// <returns>Normalized vector</returns>
public static Vector2 Normalize(Vector2 v)
{
v.Normalize();
return v;
}
/// <summary>
/// Calculate distence between vectors
/// </summary>
/// <param name="from">the start vector</param>
/// <param name="to">the end vector</param>
/// <returns>Distance between from and to vectors</returns>
public static float Distance(Vector2 from, Vector2 to)
{
float xdiff = from.x - to.x;
float ydiff = from.y - to.y;
return (float)Math.Sqrt(Math.Pow(xdiff, 2) + Math.Pow(ydiff, 2));
}
#endregion
}
}
} <<反饋>> |
Beta Was this translation helpful? Give feedback.
-
七七(程式) 我的答案public class Question1
{
public struct Vector2
{
/// <summary>
/// The tolerance of the float value
/// </summary>
private const float kEpsilon = 1E-05f;
#region -- Constant Vectors --
public static readonly Vector2 Zero = new Vector2(0, 0);
public static readonly Vector2 One = new Vector2(1, 1);
public static readonly Vector2 Left = new Vector2(-1, 0);
public static readonly Vector2 Right = new Vector2(1, 0);
public static readonly Vector2 Up = new Vector2(0, 1);
public static readonly Vector2 Down = new Vector2(0, -1);
#endregion
/// <summary>
/// x-axis
/// </summary>
public float x;
/// <summary>
/// y-axis
/// </summary>
public float y;
/// <summary>
/// Length of vector
/// </summary>
public float magnitude
{
get { return (float)Math.Sqrt(x * x + y * y); }
}
#region -- Constructor --
/// <summary>
/// Constructor for newing vector
/// </summary>
/// <param name="x">init x-axis</param>
/// <param name="y">init y-axis</param>
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
#endregion
/// <summary>
/// Normalize current vector
/// </summary>
public void Normalize()
{
// x = x / this.magnitude;
// y = y / this.magnitude; 修正前
// 修正後
// if (magnitude < kEpsilon) 又錯!?
// {
// x = 0;
// y = 0;
// }
// else
// {
// x = x / magnitude;
// y = y / magnitude;
// }
float len = magnitude; //看老師的修改,好像是magnitude小數點每次計算的誤差導致錯誤了
if (len < kEpsilon) //確實這樣修改不用算那麼多次,而且測試才會過
{
x = 0;
y = 0;
}
else
{
x /= len;
y /= len;
}
}
/// <summary>
/// Calculate current length of vector
/// </summary>
/// <returns>length of vector</returns>
public float Magnitude()
{
return (float)Math.Sqrt(x * x + y * y);
}
/// <summary>
/// Revert current vector
/// </summary>
public void Negative()
{
x = -x;
y = -y;
}
/// <summary>
/// Add other vector
/// </summary>
public void Add(Vector2 v)
{
x += v.x;
y += v.y;
}
/// <summary>
/// Subtract other vector
/// </summary>
public void Subtract(Vector2 v)
{
x -= v.x;
y -= v.y;
}
#region -- Static Methods --
/// <summary>
/// Normalize specific vector
/// </summary>
/// <param name="v">the specific vector to normalize</param>
/// <returns>Normalized vector</returns>
public static Vector2 Normalize(Vector2 v)
{
float magnitude = (float)Math.Sqrt(v.x * v.x + v.y * v.y);
return new Vector2(v.x / magnitude, v.y / magnitude);
}
/// <summary>
/// Calculate distence between vectors
/// </summary>
/// <param name="from">the start vector</param>
/// <param name="to">the end vector</param>
/// <returns>Distance between from and to vectors</returns>
public static float Distance(Vector2 from, Vector2 to)
{
return (float)
Math.Sqrt((Math.Pow(from.x - to.x, 2.0)) + Math.Pow(from.y - to.y, 2.0));
}
#endregion
}
} ٩(๑•̀ω•́๑)۶ |
Beta Was this translation helpful? Give feedback.
-
TWEdward(新人/程式/企劃) 我的答案public struct Vector2
{
/// <summary>
/// The tolerance of the float value
/// </summary>
private const float kEpsilon = 1E-05f;
#region -- Constant Vectors --
public static readonly Vector2 Zero = new Vector2(0, 0);
public static readonly Vector2 One = new Vector2(1, 1);
public static readonly Vector2 Left = new Vector2(-1, 0);
public static readonly Vector2 Right = new Vector2(1, 0);
public static readonly Vector2 Up = new Vector2(0, 1);
public static readonly Vector2 Down = new Vector2(0, -1);
#endregion
/// <summary>
/// x-axis
/// </summary>
public float x;
/// <summary>
/// y-axis
/// </summary>
public float y;
/// <summary>
/// Length of vector
/// </summary>
public float magnitude { get; }
#region -- Constructor --
/// <summary>
/// Constructor for newing vector
/// </summary>
/// <param name="x">init x-axis</param>
/// <param name="y">init y-axis</param>
public Vector2(float x, float y)
{
// throw new NotImplementedException();
this.x = x;
this.y = y;
magnitude= (float)Math.Sqrt(x * x + y * y);
}
#endregion
/// <summary>
/// Normalize current vector
/// </summary>
public void Normalize()
{
// throw new NotImplementedException();
float length = Magnitude();
if(length>0)
{
x /= length;
y /= length;
}
}
/// <summary>
/// Calculate current length of vector
/// </summary>
/// <returns>length of vector</returns>
public float Magnitude()
{
// throw new NotImplementedException();
return (float)Math.Sqrt(x*x + y*y);
}
/// <summary>
/// Revert current vector
/// </summary>
public void Negative()
{
// throw new NotImplementedException();
x = -x;
y = -y;
}
/// <summary>
/// Add other vector
/// </summary>
public void Add(Vector2 v)
{
// throw new NotImplementedException();
x += v.x;
y += v.y;
}
/// <summary>
/// Subtract other vector
/// </summary>
public void Subtract(Vector2 v)
{
// throw new NotImplementedException();
x -= v.x;
y -= v.y;
}
#region -- Static Methods --
/// <summary>
/// Normalize specific vector
/// </summary>
/// <param name="v">the specific vector to normalize</param>
/// <returns>Normalized vector</returns>
public static Vector2 Normalize(Vector2 v)
{
// throw new NotImplementedException();
float length=v.Magnitude();
return new Vector2(v.x/length, v.y/length);
}
/// <summary>
/// Calculate distence between vectors
/// </summary>
/// <param name="from">the start vector</param>
/// <param name="to">the end vector</param>
/// <returns>Distance between from and to vectors</returns>
public static float Distance(Vector2 from, Vector2 to)
{
// throw new NotImplementedException();
float dx=to.x-from.x;
float dy=to.y-from.y;
return (float)Math.Sqrt(dx*dx+dy*dy);
}
#endregion
} 我解題的心得感想這次的題目跟數學的向量有關,而我數學已經還給學校老師了(汗),因此我就花了些時間,上網查找有關向量的數學知識,以及研究怎麼實作向量計算的程式碼寫法,也讓我越來越體會到要掌握程式軟工這個專長技能,相關的數學知識跟英文名詞多少也要知道。謝謝老師出的這一題有讓我認識到向量程式碼的實作寫法,也把還給學校的數學重新複習一遍(汗)><" |
Beta Was this translation helpful? Give feedback.
-
小鲨 (程式) 答案public class Question1
{
public struct Vector2
{
/// <summary>
/// The tolerance of the float value
/// </summary>
private const float kEpsilon = 1E-05f;
#region -- Constant Vectors --
public static readonly Vector2 Zero = new Vector2(0, 0);
public static readonly Vector2 One = new Vector2(1, 1);
public static readonly Vector2 Left = new Vector2(-1, 0);
public static readonly Vector2 Right = new Vector2(1, 0);
public static readonly Vector2 Up = new Vector2(0, 1);
public static readonly Vector2 Down = new Vector2(0, -1);
#endregion
/// <summary>
/// x-axis
/// </summary>
public float x;
/// <summary>
/// y-axis
/// </summary>
public float y;
/// <summary>
/// Length of vector
/// </summary>
public float magnitude { get; }
#region -- Constructor --
/// <summary>
/// Constructor for newing vector
/// </summary>
/// <param name="x">init x-axis</param>
/// <param name="y">init y-axis</param>
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
magnitude = (float)Math.Sqrt(x * x + y * y);
}
#endregion
/// <summary>
/// Normalize current vector
/// </summary>
public void Normalize()
{
if (magnitude < kEpsilon)
{
x = 0;
y = 0;
}
else
{
x = x / magnitude;
y = y / magnitude;
}
}
/// <summary>
/// Calculate current length of vector
/// </summary>
/// <returns>length of vector</returns>
public float Magnitude()
{
return magnitude;
}
/// <summary>
/// Revert current vector
/// </summary>
public void Negative()
{
x = -x;
y = -y;
}
/// <summary>
/// Add other vector
/// </summary>
public void Add(Vector2 v)
{
x += v.x;
y += v.y;
}
/// <summary>
/// Subtract other vector
/// </summary>
public void Subtract(Vector2 v)
{
x -= v.x;
y -= v.y;
}
#region -- Static Methods --
/// <summary>
/// Normalize specific vector
/// </summary>
/// <param name="v">the specific vector to normalize</param>
/// <returns>Normalized vector</returns>
public static Vector2 Normalize(Vector2 v)
{
if (v.magnitude < kEpsilon)
{
return Vector2.Zero;
}
return new Vector2(v.x / v.magnitude, v.y / v.magnitude);
}
/// <summary>
/// Calculate distence between vectors
/// </summary>
/// <param name="from">the start vector</param>
/// <param name="to">the end vector</param>
/// <returns>Distance between from and to vectors</returns>
public static float Distance(Vector2 from, Vector2 to)
{
return (float)Math.Sqrt(Math.Pow(to.x - from.x, 2) + Math.Pow(to.y - from.y, 2));
}
#endregion
}
} |
Beta Was this translation helpful? Give feedback.
-
肥羊(程式) 我的答案public class Question1
{
public struct Vector2
{
/// <summary>
/// The tolerance of the float value
/// </summary>
private const float kEpsilon = 1E-05f;
#region -- Constant Vectors --
public static readonly Vector2 Zero = new Vector2(0, 0);
public static readonly Vector2 One = new Vector2(1, 1);
public static readonly Vector2 Left = new Vector2(-1, 0);
public static readonly Vector2 Right = new Vector2(1, 0);
public static readonly Vector2 Up = new Vector2(0, 1);
public static readonly Vector2 Down = new Vector2(0, -1);
#endregion
/// <summary>
/// x-axis
/// </summary>
public float x;
/// <summary>
/// y-axis
/// </summary>
public float y;
/// <summary>
/// Length of vector
/// </summary>
public float magnitude //返回向量长度为 (x*x+y*y) 的平方根
{
get
{
return (float)Math.Sqrt(x * x + y * y);
}
}
#region -- Constructor --
/// <summary>
/// Constructor for newing vector
/// </summary>
/// <param name="x">init x-axis</param>
/// <param name="y">init y-axis</param>
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
#endregion
/// <summary>
/// Normalize current vector
/// </summary>
public void Normalize() //歸一化 返回向量1,若小於1則返回0
{
// v_normalized = v / ||v||,||v||是向量長度[平方根]的符號 公式是sqrt(v1^2 + v2^2)
float leng = magnitude;
if (leng >= kEpsilon)
{
x /= leng;
y /= leng;
}
else
{
x = 0;
y = 0;
}
}
/// <summary>
/// Calculate current length of vector
/// </summary>
/// <returns>length of vector</returns>
public float Magnitude() //返回向量长度为 (x*x+y*y) 的平方根
{
return (float)Math.Sqrt(x * x + y * y);
}
/// <summary>
/// Revert current vector
/// </summary>
public void Negative() //正值->負值[正負得負],負值->正值[負負得正]
{
x *= -1;
y *= -1;
}
/// <summary>
/// Add other vector
/// </summary>
public void Add(Vector2 v)
{
x += v.x;
y += v.y;
}
/// <summary>
/// Subtract other vector
/// </summary>
public void Subtract(Vector2 v)
{
x -= v.x;
y -= v.y;
}
#region -- Static Methods --
/// <summary>
/// Normalize specific vector
/// </summary>
/// <param name="v">the specific vector to normalize</param>
/// <returns>Normalized vector</returns>
public static Vector2 Normalize(Vector2 v)
{
v.Normalize();
return v;
}
/// <summary>
/// Calculate distence between vectors
/// </summary>
/// <param name="from">the start vector</param>
/// <param name="to">the end vector</param>
/// <returns>Distance between from and to vectors</returns>
public static float Distance(Vector2 from, Vector2 to)//公式distance = sqrt((a.x - b.x)^2 + (a.y - b.y)^2)
{
float xDiff = to.x - from.x;
float yDiff = to.y - from.y;
return (float)Math.Sqrt((xDiff * xDiff) + (yDiff * yDiff));
}
#endregion
}
} Normalize有看到kEpsilon但Unity API沒有特別提就去爬一下,原來是用浮點數定義的距離 |
Beta Was this translation helpful? Give feedback.
-
答案public float magnitude { get => Magnitude(); }
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
public void Normalize()
{
this = Vector2.Normalize(this);
}
public float Magnitude()
{
return Mathf.Sqrt(x * x + y * y);
}
public void Negative()
{
x = -x;
y = -y;
}
public void Add(Vector2 v)
{
x += v.x;
y += v.y;
}
public void Subtract(Vector2 v)
{
x -= v.x;
y -= v.y;
}
public static Vector2 Normalize(Vector2 v)
{
float length = v.Magnitude();
if (length > kEpsilon)
{
v.x = v.x / length;
v.y = v.y / length;
}
else
{
v.x = 0;
v.y = 0;
}
return v;
}
public static float Distance(Vector2 from, Vector2 to)
{
to.Subtract(from);
return to.magnitude;
} |
Beta Was this translation helpful? Give feedback.
-
Tina( Unity程式 | 佛系劇本 ) 我的答案using System;
namespace PG0003.Questions
{
public class Question1
{
public struct Vector2
{
/// <summary>
/// The tolerance of the float value
/// </summary>
private const float kEpsilon = 1E-05f;
#region -- Constant Vectors --
public static readonly Vector2 Zero = new Vector2(0, 0);
public static readonly Vector2 One = new Vector2(1, 1);
public static readonly Vector2 Left = new Vector2(-1, 0);
public static readonly Vector2 Right = new Vector2(1, 0);
public static readonly Vector2 Up = new Vector2(0, 1);
public static readonly Vector2 Down = new Vector2(0, -1);
#endregion
/// <summary>
/// x-axis
/// </summary>
public float x;
/// <summary>
/// y-axis
/// </summary>
public float y;
/// <summary>
/// Length of vector
/// </summary>
public float magnitude
{
get
{
return (float) Math.Sqrt(x * x + y * y);
}
}
#region -- Constructor --
/// <summary>
/// Constructor for newing vector
/// </summary>
/// <param name="x">init x-axis</param>
/// <param name="y">init y-axis</param>
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
#endregion
/// <summary>
/// Normalize current vector
/// </summary>
public void Normalize()
{
float len = (float)Math.Sqrt(x * x + y * y);
if (len > kEpsilon)
{
x /= len;
y /= len;
}
else
{
x = 0;
y = 0;
}
}
/// <summary>
/// Calculate current length of vector
/// </summary>
/// <returns>length of vector</returns>
public float Magnitude()
{
return (float) Math.Sqrt(x * x + y * y);
}
/// <summary>
/// Revert current vector
/// </summary>
public void Negative()
{
x = -x;
y = -y;
}
/// <summary>
/// Add other vector
/// </summary>
public void Add(Vector2 v)
{
x += v.x;
y += v.y;
}
/// <summary>
/// Subtract other vector
/// </summary>
public void Subtract(Vector2 v)
{
x -= v.x;
y -= v.y;
}
#region -- Static Methods --
/// <summary>
/// Normalize specific vector
/// </summary>
/// <param name="v">the specific vector to normalize</param>
/// <returns>Normalized vector</returns>
public static Vector2 Normalize(Vector2 v)
{
v.Normalize();
return v;
}
/// <summary>
/// Calculate distence between vectors
/// </summary>
/// <param name="from">the start vector</param>
/// <param name="to">the end vector</param>
/// <returns>Distance between from and to vectors</returns>
public static float Distance(Vector2 from, Vector2 to)
{
float xDis = from.x - to.x;
float yDis = from.y - to.y;
return (float) Math.Sqrt(xDis * xDis + yDis * yDis);
}
#endregion
}
}
} 微積分補考完後就全部都還給老師了哈哈 |
Beta Was this translation helpful? Give feedback.
-
歐雷(程式) 答案 public class Question1
{
public struct Vector2
{
/// <summary>
/// The tolerance of the float value
/// </summary>
private const float kEpsilon = 1E-05f;
#region -- Constant Vectors --
public static readonly Vector2 Zero = new Vector2(0, 0);
public static readonly Vector2 One = new Vector2(1, 1);
public static readonly Vector2 Left = new Vector2(-1, 0);
public static readonly Vector2 Right = new Vector2(1, 0);
public static readonly Vector2 Up = new Vector2(0, 1);
public static readonly Vector2 Down = new Vector2(0, -1);
#endregion
/// <summary>
/// x-axis
/// </summary>
public float x;
/// <summary>
/// y-axis
/// </summary>
public float y;
/// <summary>
/// Length of vector
/// </summary>
public float magnitude
{ get
{
return (float)Math.Sqrt(x * x + y * y);
}
}
#region -- Constructor --
/// <summary>
/// Constructor for newing vector
/// </summary>
/// <param name="x">init x-axis</param>
/// <param name="y">init y-axis</param>
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
#endregion
/// <summary>
/// Normalize current vector
/// </summary>
public void Normalize()
{
float len = magnitude;
if (len > kEpsilon)
{ x /= len; y /= len; }
else
{ x = 0; y = 0; }
}
/// <summary>
/// Calculate current length of vector
/// </summary>
/// <returns>length of vector</returns>
public float Magnitude()
{
return (float)Math.Sqrt(x * x + y * y);
}
/// <summary>
/// Revert current vector
/// </summary>
public void Negative()
{
x = -x;
y = -y;
}
/// <summary>
/// Add other vector
/// </summary>
public void Add(Vector2 v)
{
x += v.x;
y += v.y;
}
/// <summary>
/// Subtract other vector
/// </summary>
public void Subtract(Vector2 v)
{
x -= v.x;
y -= v.y;
}
#region -- Static Methods --
/// <summary>
/// Normalize specific vector
/// </summary>
/// <param name="v">the specific vector to normalize</param>
/// <returns>Normalized vector</returns>
public static Vector2 Normalize(Vector2 v)
{
v.Normalize();
return v;
}
/// <summary>
/// Calculate distence between vectors
/// </summary>
/// <param name="from">the start vector</param>
/// <param name="to">the end vector</param>
/// <returns>Distance between from and to vectors</returns>
public static float Distance(Vector2 from, Vector2 to)
{
float _x = from.x - to.x;
float _y = from.y - to.y;
return (float)Math.Sqrt(_x * _x + _y * _y);
}
#endregion
}
} |
Beta Was this translation helpful? Give feedback.
-
極光(程式) 我的答案public struct Vector2
{
/// <summary>
/// The tolerance of the float value
/// </summary>
private const float kEpsilon = 1E-05f;
#region -- Constant Vectors --
public static readonly Vector2 Zero = new Vector2(0, 0);
public static readonly Vector2 One = new Vector2(1, 1);
public static readonly Vector2 Left = new Vector2(-1, 0);
public static readonly Vector2 Right = new Vector2(1, 0);
public static readonly Vector2 Up = new Vector2(0, 1);
public static readonly Vector2 Down = new Vector2(0, -1);
#endregion
/// <summary>
/// x-axis
/// </summary>
public float x;
/// <summary>
/// y-axis
/// </summary>
public float y;
/// <summary>
/// Length of vector
/// </summary>
public float magnitude
{
get
{
return (float)Math.Sqrt(x * x + y * y);
}
}
#region -- Constructor --
/// <summary>
/// Constructor for newing vector
/// </summary>
/// <param name="x">init x-axis</param>
/// <param name="y">init y-axis</param>
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
#endregion
/// <summary>
/// Normalize current vector
/// </summary>
public void Normalize()
{
float len = (float)Math.Sqrt(x * x + y * y);
if (len > kEpsilon)
{
x /= len; y /= len;
}
else
{
x = 0; y = 0;
}
}
/// <summary>
/// Calculate current length of vector
/// </summary>
/// <returns>length of vector</returns>
public float Magnitude()
{
return (float)Math.Sqrt(x * x + y * y);
}
/// <summary>
/// Revert current vector
/// </summary>
public void Negative()
{
x = -x;
y = -y;
}
/// <summary>
/// Add other vector
/// </summary>
public void Add(Vector2 v)
{
x += v.x;
y += v.y;
}
/// <summary>
/// Subtract other vector
/// </summary>
public void Subtract(Vector2 v)
{
x -= v.x;
y -= v.y;
}
#region -- Static Methods --
/// <summary>
/// Normalize specific vector
/// </summary>
/// <param name="v">the specific vector to normalize</param>
/// <returns>Normalized vector</returns>
public static Vector2 Normalize(Vector2 v)
{
v.Normalize();
return v;
}
/// <summary>
/// Calculate distence between vectors
/// </summary>
/// <param name="from">the start vector</param>
/// <param name="to">the end vector</param>
/// <returns>Distance between from and to vectors</returns>
public static float Distance(Vector2 from, Vector2 to)
{
return new Vector2(to.x - from.x, to.y - from.y).magnitude;
}
#endregion
} 這次的題目花了比較久的時間去寫,上完課程之後邊做邊翻影片才做出來的。 |
Beta Was this translation helpful? Give feedback.
-
Introduction
實作Unity的Vector2...
Answer
答案
需要一點數學知識...
Beta Was this translation helpful? Give feedback.
All reactions