Replies: 16 comments 5 replies
-
小鲨 (程式) 答案public class Question1
{
public struct Vector3
{
public float x; public float y; public float z;
public Vector3(float x, float y, float z)
{
this.x = x; this.y = y; this.z = z;
}
}
public struct Vector2
{
const float kEpsilon = 1E-05f;
public float x, y;
#region -- Constructor --
public Vector2(float x, float y)
{
this.x = x; this.y = y;
}
#endregion
#region -- Operators --
/// <summary>
/// Plus two vector
/// </summary>
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x + v2.x, v1.y + v2.y);
}
/// <summary>
/// Subtract two vector
/// </summary>
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x - v2.x, v1.y - v2.y);
}
/// <summary>
/// Multiple vector by float
/// </summary>
public static Vector2 operator *(Vector2 v, float f)
{
return new Vector2(v.x * f, v.y * f);
}
/// <summary>
/// Divide vector by float
/// </summary>
/// <returns></returns>
/// <exception cref="ArgumentException">f is zero</exception>
public static Vector2 operator /(Vector2 v, float f)
{
if (f == 0)
{
throw new ArgumentException("f is zero");
}
return new Vector2(v.x / f, v.y / f);
}
#endregion
#region -- Conversion --
/// <summary>
/// Convert Vector3 to Vector2
/// </summary>
public static implicit operator Vector2(Vector3 v)
{
return new Vector2(v.x, v.y);
}
/// <summary>
/// Convert Vector2 to Vector3
/// </summary>
/// <param name="v"></param>
public static explicit operator Vector3(Vector2 v)
{
return new Vector3(v.x, v.y, 0);
}
#endregion
/// <summary>
/// Custom my-format string
/// </summary>
public override string ToString()
{
return $"({x}, {y})";
}
}
} |
Beta Was this translation helpful? Give feedback.
-
JIA(程式新手) 我的答案#region -- Operators --
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x + v2.x, v1.y + v2.y);
}
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x - v2.x, v1.y - v2.y);
}
public static Vector2 operator *(Vector2 v, float f)
{
return new Vector2(v.x * f, v.y * f);
}
public static Vector2 operator /(Vector2 v, float f)
{
if (Math.Abs(f) < kEpsilon) throw new ArgumentException("f equal zero");
return new Vector2(v.x / f, v.y / f);
}
#endregion
#region -- Conversion --
public static implicit operator Vector2(Vector3 v)
{
return new Vector2(v.x, v.y);
}
public static explicit operator Vector3(Vector2 v)
{
return new Vector3(v.x, v.y, 0);
}
#endregion
public override string ToString()
{
return $"({x}, {y})";
} |
Beta Was this translation helpful? Give feedback.
-
老蕭OLDShaw(程式) 我的答案public class Question1
{
public struct Vector3
{
public float x; public float y; public float z;
public Vector3(float x, float y, float z)
{
this.x = x; this.y = y; this.z = z;
}
}
public struct Vector2
{
const float kEpsilon = 1E-05f;
public float x, y;
#region -- Constructor --
public Vector2(float x, float y)
{
this.x = x; this.y = y;
}
#endregion
#region -- Operators --
/// <summary>
/// Plus two vector
/// </summary>
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x + v2.x, v1.y + v2.y);
}
/// <summary>
/// Subtract two vector
/// </summary>
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x - v2.x, v1.y - v2.y);
}
/// <summary>
/// Multiple vector by float
/// </summary>
public static Vector2 operator *(Vector2 v, float f)
{
return new Vector2(v.x * f, v.y * f);
}
/// <summary>
/// Divide vector by float
/// </summary>
/// <returns></returns>
/// <exception cref="ArgumentException">f is zero</exception>
public static Vector2 operator /(Vector2 v, float f)
{
if (f == 0) throw new ArgumentException();
return new Vector2(v.x / f, v.y / f);
}
#endregion
#region -- Conversion --
/// <summary>
/// Convert Vector3 to Vector2
/// </summary>
public static implicit operator Vector2(Vector3 v)
{
return new Vector2(v.x, v.y);
}
/// <summary>
/// Convert Vector2 to Vector3
/// </summary>
/// <param name="v"></param>
public static explicit operator Vector3(Vector2 v)
{
return new Vector3(v.x, v.y, 0);
}
#endregion
/// <summary>
/// Custom my-format string
/// </summary>
public override string ToString()
{
Vector3 convertedResult = (Vector3)this;
return $"({convertedResult.x}, {convertedResult.y}, {convertedResult.z})";
}
}
} 因為平常沒在用Exception,雖然知道是用來手動丟錯誤的,但不知道除法Function前面那幾行是要做什麼,之後寫完進行測試想說0的除法的怎麼過不了,才想到除以0的值是不存在,Function傳0會出問題,後來猜說要自己丟Exception,後面才成功過測試。 |
Beta Was this translation helpful? Give feedback.
-
小4 ( 程式 ) public Vector2(float x, float y)
{
this.x = x; this.y = y;
}
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
v1.x += v2.x;
v1.y += v2.y;
return v1;
}
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
v1.x -= v2.x;
v1.y -= v2.y;
return v1;
}
public static Vector2 operator *(Vector2 v, float f)
{
v.x *= f;
v.y *= f;
return v;
}
public static Vector2 operator /(Vector2 v, float f)
{
if (f == 0)
{
throw new ArgumentException();
}
v.x /= f;
v.y /= f;
return v;
}
public static implicit operator Vector2(Vector3 v)
{
return new Vector2(v.x, v.y);
}
public static explicit operator Vector3(Vector2 v)
{
return new Vector3(v.x, v.y, 0);
}
//(1.00, 2.00)
public override string ToString()
{
return $"({x}, {y})";
} TestImplicit TestExplicit ToString是不是忘了加[TestCase] |
Beta Was this translation helpful? Give feedback.
-
答案 public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x + v2.x, v1.y + v2.y);
}
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x - v2.x, v1.y - v2.y);
}
public static Vector2 operator *(Vector2 v, float f)
{
return new Vector2(v.x * f, v.y * f);
}
public static Vector2 operator /(Vector2 v, float f)
{
if(f == 0)
{
throw new ArgumentException();
}
else
{
return new Vector2(v.x / f, v.y / f);
}
}
public static implicit operator Vector2(Vector3 v)
{
return new Vector2(v.x, v.y);
}
public static explicit operator Vector3(Vector2 v)
{
return new Vector3(v.x, v.y, 0);
}
public override string ToString()
{
return $"({x}, {y})";
} |
Beta Was this translation helpful? Give feedback.
-
Cliff Lee CL (程式、專案管理) 我的答案public class Question1
{
public struct Vector3
{
public float x; public float y; public float z;
public Vector3(float x, float y, float z)
{
this.x = x; this.y = y; this.z = z;
}
}
public struct Vector2
{
const float kEpsilon = 1E-05f;
public float x, y;
#region -- Constructor --
public Vector2(float x, float y)
{
this.x = x; this.y = y;
}
#endregion
#region -- Operators --
/// <summary>
/// Plus two vector
/// </summary>
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x + v2.x, v1.y + v2.y);
}
/// <summary>
/// Subtract two vector
/// </summary>
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x - v2.x, v1.y - v2.y);
}
/// <summary>
/// Multiple vector by float
/// </summary>
public static Vector2 operator *(Vector2 v, float f)
{
return new Vector2(v.x * f, v.y * f);
}
/// <summary>
/// Divide vector by float
/// </summary>
/// <returns></returns>
/// <exception cref="ArgumentException">f is zero</exception>
public static Vector2 operator /(Vector2 v, float f)
{
if (f == 0)
throw new ArgumentException("f is zero");
return new Vector2(v.x / f, v.y / f);
}
#endregion
#region -- Conversion --
/// <summary>
/// Convert Vector3 to Vector2
/// </summary>
public static implicit operator Vector2(Vector3 v)
{
return new Vector2(v.x, v.y);
}
/// <summary>
/// Convert Vector2 to Vector3
/// </summary>
/// <param name="v"></param>
public static explicit operator Vector3(Vector2 v)
{
return new Vector3(v.x, v.y, 0);
}
#endregion
/// <summary>
/// Custom my-format string
/// </summary>
public override string ToString()
{
return $"({x}, {y})";
}
}
} 不小心貼錯地方重貼 (? |
Beta Was this translation helpful? Give feedback.
-
Tina( Unity程式 | 佛系劇本 ) 我的答案using System;
namespace PG0004.Questions
{
public class Question1
{
public struct Vector3
{
public float x; public float y; public float z;
public Vector3(float x, float y, float z)
{
this.x = x; this.y = y; this.z = z;
}
}
public struct Vector2
{
const float kEpsilon = 1E-05f;
public float x, y;
#region -- Constructor --
public Vector2(float x, float y)
{
this.x = x; this.y = y;
}
#endregion
#region -- Operators --
/// <summary>
/// Plus two vector
/// </summary>
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x + v2.x, v1.y + v2.y);
}
/// <summary>
/// Subtract two vector
/// </summary>
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x - v2.x, v1.y - v2.y);
}
/// <summary>
/// Multiple vector by float
/// </summary>
public static Vector2 operator *(Vector2 v, float f)
{
return new Vector2(v.x * f, v.y * f);
}
/// <summary>
/// Divide vector by float
/// </summary>
/// <returns></returns>
/// <exception cref="ArgumentException">f is zero</exception>
public static Vector2 operator /(Vector2 v, float f)
{
if (Math.Abs(f) < kEpsilon)
{
throw new ArgumentException("The absolute f must be greater than 1E-05f");
}
return new Vector2(v.x / f, v.y / f);
}
#endregion
#region -- Conversion --
/// <summary>
/// Convert Vector3 to Vector2
/// </summary>
public static implicit operator Vector2(Vector3 v)
{
return new Vector2(v.x, v.y);
}
/// <summary>
/// Convert Vector2 to Vector3
/// </summary>
/// <param name="v"></param>
public static explicit operator Vector3(Vector2 v)
{
return new Vector3(v.x, v.y, 0);
}
#endregion
/// <summary>
/// Custom my-format string
/// </summary>
public override string ToString()
{
return $"{x}, {y}";
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
七七(程式) 我的答案namespace PG0004.Questions
{
public class Question1
{
public struct Vector3
{
public float x;
public float y;
public float z;
public Vector3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
}
public struct Vector2
{
const float kEpsilon = 1E-05f;
public float x,
y;
#region -- Constructor --
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
#endregion
#region -- Operators --
/// <summary>
/// Plus two vector
/// </summary>
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x + v2.x, v1.y + v2.y);
}
/// <summary>
/// Subtract two vector
/// </summary>
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x - v2.x, v1.y - v2.y);
}
/// <summary>
/// Multiple vector by float
/// </summary>
public static Vector2 operator *(Vector2 v, float f)
{
return new Vector2(v.x * f, v.y * f);
}
/// <summary>
/// Divide vector by float
/// </summary>
/// <returns></returns>
/// <exception cref="ArgumentException">f is zero</exception>
public static Vector2 operator /(Vector2 v, float f)
{
if (Math.Abs(f) < kEpsilon)
{
throw new ArgumentException("The absolute f must be greater than 1E-05f");
}
return new Vector2(v.x / f, v.y / f);
}
#endregion
#region -- Conversion --
/// <summary>
/// Convert Vector3 to Vector2
/// </summary>
public static implicit operator Vector2(Vector3 v)
{
return new Vector2(v.x, v.y);
}
/// <summary>
/// Convert Vector2 to Vector3
/// </summary>
/// <param name="v"></param>
public static explicit operator Vector3(Vector2 v)
{
return new Vector3(v.x, v.y, 0f);
}
#endregion
/// <summary>
/// Custom my-format string
/// </summary>
public override string ToString()
{
return $"({x},{y})";
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Syuan 我的答案public class Question1
{
public struct Vector3
{
public float x; public float y; public float z;
public Vector3(float x, float y, float z)
{
this.x = x; this.y = y; this.z = z;
}
}
public struct Vector2
{
const float kEpsilon = 1E-05f;
public float x, y;
#region -- Constructor --
public Vector2(float x, float y)
{
this.x = x; this.y = y;
}
#endregion
#region -- Operators --
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x + v2.x, v1.y + v2.y);
}
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x - v2.x, v1.y - v2.y);
}
public static Vector2 operator *(Vector2 v, float f)
{
return new Vector2(v.x * f, v.y * f);
}
public static Vector2 operator /(Vector2 v, float f)
{
if (Math.Abs(f) < kEpsilon)
{
throw new ArgumentException("The absolute f must be greater than 1E-05f");
}
return new Vector2(v.x / f, v.y / f);
}
#endregion
#region -- Conversion --
public static implicit operator Vector2(Vector3 v)
{
return new Vector2(v.x, v.y);
}
public static explicit operator Vector3(Vector2 v)
{
return new Vector3(v.x, v.y, 0);
}
#endregion
public override string ToString()
{
return $"({x}, {y})";
}
}
} |
Beta Was this translation helpful? Give feedback.
-
罐頭(程式) 我的答案ヾ(•ω•`)onamespace PG0004.Questions
{
public class Question1
{
public struct Vector3
{
public float x;
public float y;
public float z;
public Vector3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
}
public struct Vector2
{
const float kEpsilon = 1E-05f;
public float x, y;
#region -- Constructor --
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
#endregion
#region -- Operators --
/// <summary>
/// Plus two vector
/// </summary>
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x + v2.x, v1.y + v2.y);
}
/// <summary>
/// Subtract two vector
/// </summary>
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x - v2.x, v1.y - v2.y);
}
/// <summary>
/// Multiple vector by float
/// </summary>
public static Vector2 operator *(Vector2 v, float f)
{
return new Vector2(v.x * f, v.y * f);
}
/// <summary>
/// Divide vector by float
/// </summary>
/// <returns></returns>
/// <exception cref="ArgumentException">f is zero</exception>
public static Vector2 operator /(Vector2 v, float f)
{
if (Math.Abs(f) < kEpsilon)
{
throw new ArgumentException("The absolute f must be greater than 1E-05f");
}
return new Vector2(v.x / f, v.y / f);
}
#endregion
#region -- Conversion --
/// <summary>
/// Convert Vector3 to Vector2
/// </summary>
public static implicit operator Vector2(Vector3 v)
{
return new Vector2(v.x, v.y);
}
/// <summary>
/// Convert Vector2 to Vector3
/// </summary>
/// <param name="v"></param>
public static explicit operator Vector3(Vector2 v)
{
return new Vector3(v.x, v.y, 0);
}
#endregion
/// <summary>
/// Custom my-format string
/// </summary>
public override string ToString()
{
return $"({x},{y})";
}
}
}
} 感覺上次的懂了後這次的順好多歐,超級有成就感~φ(゜▽゜*)♪ |
Beta Was this translation helpful? Give feedback.
-
肥羊(程式) 我的答案public class Question1
{
public struct Vector3
{
public float x; public float y; public float z;
public Vector3(float x, float y, float z)
{
this.x = x; this.y = y; this.z = z;
}
}
public struct Vector2
{
const float kEpsilon = 1E-05f;
public float x, y;
#region -- Constructor --
public Vector2(float x, float y)
{
this.x = x; this.y = y;
}
#endregion
#region -- Operators --
/// <summary>
/// Plus two vector
/// </summary>
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x + v2.x, v1.y + v2.y);
}
/// <summary>
/// Subtract two vector
/// </summary>
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x - v2.x, v1.y - v2.y);
}
/// <summary>
/// Multiple vector by float
/// </summary>
public static Vector2 operator *(Vector2 v, float f)
{
return new Vector2(v.x * f, v.y * f);
}
/// <summary>
/// Divide vector by float
/// </summary>
/// <returns></returns>
/// <exception cref="ArgumentException">f is zero</exception>
public static Vector2 operator /(Vector2 v, float f)
{
if (f == 0) throw new ArgumentException("f is Zero"); //丟出報錯 訊息f is Zero
return new Vector2(v.x / f, v.y / f);
}
#endregion
#region -- Conversion --
/// <summary>
/// Convert Vector3 to Vector2
/// </summary>
public static implicit operator Vector2(Vector3 v)
{
return new Vector2(v.x, v.y);
}
/// <summary>
/// Convert Vector2 to Vector3
/// </summary>
/// <param name="v"></param>
public static explicit operator Vector3(Vector2 v)
{
return new Vector3(v.x, v.y, 0);
}
#endregion
/// <summary>
/// Custom my-format string
/// </summary>
public override string ToString()
{
return $"{x}, {y}";
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Snoweve (程式) 我的答案
<<反饋>> |
Beta Was this translation helpful? Give feedback.
-
TWEdward(新人/程式/企劃) 我的答案public class Question1
{
public struct Vector3
{
public float x; public float y; public float z;
public Vector3(float x, float y, float z)
{
this.x = x; this.y = y; this.z = z;
}
}
public struct Vector2
{
const float kEpsilon = 1E-05f;
public float x, y;
#region -- Constructor --
public Vector2(float x, float y)
{
this.x = x; this.y = y;
}
#endregion
#region -- Operators --
/// <summary>
/// Plus two vector
/// </summary>
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x + v2.x, v1.y + v2.y);
}
/// <summary>
/// Subtract two vector
/// </summary>
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x - v2.x, v1.y - v2.y);
}
/// <summary>
/// Multiple vector by float
/// </summary>
public static Vector2 operator *(Vector2 v, float f)
{
return new Vector2(v.x * f, v.y * f);
}
/// <summary>
/// Divide vector by float
/// </summary>
/// <returns></returns>
/// <exception cref="ArgumentException">f is zero</exception>
public static Vector2 operator /(Vector2 v, float f)
{
if (Math.Abs(f) < kEpsilon)
{
throw new ArgumentException("The absolute f must be greater than 1E-05f");
}
return new Vector2(v.x / f, v.y / f);
}
#endregion
#region -- Conversion --
/// <summary>
/// Convert Vector3 to Vector2
/// </summary>
public static implicit operator Vector2(Vector3 v)
{
return new Vector2(v.x, v.y);
}
/// <summary>
/// Convert Vector2 to Vector3
/// </summary>
/// <param name="v"></param>
public static explicit operator Vector3(Vector2 v)
{
return new Vector3(v.x,v.y,0f);
}
#endregion
/// <summary>
/// Custom my-format string
/// </summary>
public override string ToString()
{
return "(" + x + "," + y + ")";
}
}
} 解題心得感想謝謝老師的用心教學以及題目練習,我有感覺到有增加新的Know-how進來了,我的技能經驗值增加了,謝謝老師,讚^_^ |
Beta Was this translation helpful? Give feedback.
-
肉鬆(程式 / 音樂) 我的答案 public struct Vector2
{
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x + v2.x, v1.y + v2.y);
}
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x - v2.x, v1.y - v2.y);
}
public static Vector2 operator *(Vector2 v, float f)
{
return new Vector2(v.x * f, v.y * f);
}
public static Vector2 operator /(Vector2 v, float f)
{
if (Math.Abs(f) < kEpsilon)
{
throw new ArgumentException("The absolute f must be greater than 1E-05f");
}
return new Vector2(v.x / f, v.y / f);
}
public static implicit operator Vector2(Vector3 v)
{
return new Vector2(v.x, v.y);
}
public static explicit operator Vector3(Vector2 v)
{
return new Vector3(v.x, v.y, 0);
}
public override string ToString()
{
return $"({x},{y})";
}
} 課程回饋override的$符號好酷喔xD |
Beta Was this translation helpful? Give feedback.
-
歐雷(程式) 答案 public class Question1
{
public struct Vector3
{
public float x; public float y; public float z;
public Vector3(float x, float y, float z)
{
this.x = x; this.y = y; this.z = z;
}
}
public struct Vector2
{
const float kEpsilon = 1E-05f;
public float x, y;
#region -- Constructor --
public Vector2(float x, float y)
{
this.x = x; this.y = y;
}
#endregion
#region -- Operators --
/// <summary>
/// Plus two vector
/// </summary>
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x + v2.x, v1.y + v2.y);
}
/// <summary>
/// Subtract two vector
/// </summary>
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x - v2.x, v1.y - v2.y);
}
/// <summary>
/// Multiple vector by float
/// </summary>
public static Vector2 operator *(Vector2 v, float f)
{
return new Vector2(v.x * f, v.y * f);
}
/// <summary>
/// Divide vector by float
/// </summary>
/// <returns></returns>
/// <exception cref="ArgumentException">f is zero</exception>
public static Vector2 operator /(Vector2 v, float f)
{
if (Math.Abs(f) < kEpsilon)
{
throw new ArgumentException("The absolute f must be greater than 1E-05f");
}
return new Vector2(v.x / f, v.y / f);
}
#endregion
#region -- Conversion --
/// <summary>
/// Convert Vector3 to Vector2
/// </summary>
public static implicit operator Vector2(Vector3 v)
{
return new Vector2(v.x, v.y);
}
/// <summary>
/// Convert Vector2 to Vector3
/// </summary>
/// <param name="v"></param>
public static explicit operator Vector3(Vector2 v)
{
return new Vector3(v.x, v.y,0);
}
#endregion
/// <summary>
/// Custom my-format string
/// </summary>
public override string ToString()
{
return $"({x},{y})";
}
}
} |
Beta Was this translation helpful? Give feedback.
-
極光(程式) 我的答案public class Question1
{
public struct Vector3
{
public float x; public float y; public float z;
public Vector3(float x, float y, float z)
{
this.x = x; this.y = y; this.z = z;
}
}
public struct Vector2
{
const float kEpsilon = 1E-05f;
public float x, y;
#region -- Constructor --
public Vector2(float x, float y)
{
this.x = x; this.y = y;
}
#endregion
#region -- Operators --
/// <summary>
/// Plus two vector
/// </summary>
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x + v2.x, v1.y + v2.y);
}
/// <summary>
/// Subtract two vector
/// </summary>
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x - v2.x, v1.y - v2.y);
}
/// <summary>
/// Multiple vector by float
/// </summary>
public static Vector2 operator *(Vector2 v, float f)
{
return new Vector2(v.x * f, v.y *f);
}
/// <summary>
/// Divide vector by float
/// </summary>
/// <returns></returns>
/// <exception cref="ArgumentException">f is zero</exception>
public static Vector2 operator /(Vector2 v, float f)
{
if (Math.Abs(f) < kEpsilon)
{
throw new ArgumentException("The absolute f must be greater than 1E-05f");
}
else
{
return new Vector2(v.x / f, v.y / f);
}
}
#endregion
#region -- Conversion --
/// <summary>
/// Convert Vector3 to Vector2
/// </summary>
public static implicit operator Vector2(Vector3 v)
{
return new Vector2(v.x, v.y);
}
/// <summary>
/// Convert Vector2 to Vector3
/// </summary>
/// <param name="v"></param>
public static explicit operator Vector3(Vector2 v)
{
return new Vector3(v.x, v.y,0);
}
#endregion
/// <summary>
/// Custom my-format string
/// </summary>
public override string ToString()
{
return $"({x},{y})";
}
}
} 在寫除法的時候不小心把 throw new ArgumentException 刪掉 讓我想了一陣子然後看題目報說答案是 ArgumentException |
Beta Was this translation helpful? Give feedback.
-
Introduction
同上次的題目,這次新增了自訂運算子的練習,下載後實作下列方法
Answer
答案
主要就練習寫operator overloading,寫到這裡其實你已經把UnityEngine的Vector2實作做得差不多了!
Beta Was this translation helpful? Give feedback.
All reactions