Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Calculate the vector AB</strong></p> <p>First define the vector from point A(1,-1) to point B(2,4) substracting A from B. The vector would be Vab(1,5).</p> <p><strong>Calculate the length of AB</strong></p> <p>Use Pythagorean theorem to calculate the length of vector AB.</p> <pre><code>|Vab| = SQRT(1²+5²) </code></pre> <p>The Length is (rounded) 5.1</p> <p><strong>Calculate the unit vector</strong></p> <p>Divide the vector by its length to get the unit vector (the vector with length 1).</p> <pre><code>V1(1/5.1,5/5.1) = V1(0.2, 0.98) </code></pre> <p><strong>Calculate the vector with length 4</strong></p> <p>Now multiply V1 with the length you want, for example 4, to get Vt.</p> <pre><code>Vt(0.2*4,0.98*4) = Vt(0.8,3.92) </code></pre> <p><strong>Calculate target point</strong></p> <p>Add the vector Vt to point A to get point T (target).</p> <pre><code>T = A + Vt = T(1.8,2.92) </code></pre> <p><strong>EDIT: Answer to your edit</strong></p> <p>The method LengthOfHypotenuse should look like that </p> <ul> <li>fixed an error on calculating bSq</li> <li>and removed redundant Math.Abs call, because a pow of 2 is always positive</li> <li>removed the addition of 0.5, don't know why you would need that</li> <li><p>you should at least use a float as return value (double or decimal would work also)</p> <pre><code>//You should work with Vector2 class instead of Point and use their Length property private double LengthOfHypotenuse(Point a, Point b) { double aSq = Math.Pow(a.X - b.X, 2); // horizontal length squared double bSq = Math.Pow(a.Y - b.Y, 2); // vertical length squared return Math.Sqrt(aSq + bSq); // length of the hypotenuse } </code></pre></li> </ul> <p>The method Draw(Point a, Point b) should look like that:</p> <ul> <li><p>Corrected DrawCell() call</p> <pre><code>private void Draw(Point a, Point b) { double maxDistance = LengthOfHypotenuse(a, b); for (int distance = 0; distance &lt; maxDistance; ++distance) { var point = CalculatePoint(new Vector2(a), new Vector2(b), distance); DrawCell(point.X, point.Y, _theLineBrush); } } </code></pre></li> </ul> <p>Your CalculatePoint(Point a, Point b, int distance) method:</p> <ul> <li><p>Moved some calculations into Vector2 class</p> <pre><code>private Point CalculatePoint(Vector2 a, Vector2 b, int distance) { Vector2 vectorAB = a - b; return a + vectorAB.UnitVector * distance; } </code></pre></li> </ul> <p>I have extended the Vector class for you to add the missing operators (credits to AgentFire)</p> <pre><code> //AgentFire: Better approach (you can rename the struct if you need): struct Vector2 { public readonly double X; public readonly double Y; public Vector2(Point p) : this(p.X,p.Y) { } public Vector2(double x, double y) { this.X = x; this.Y = y; } public static Vector2 operator -(Vector2 a, Vector2 b) { return new Vector2(b.X - a.X, b.Y - a.Y); } public static Vector2 operator +(Vector2 a, Vector2 b) { return new Vector2(b.X + a.X, b.Y + a.Y); } public static Vector2 operator *(Vector2 a, double d) { return new Vector2(a.X * d, a.Y * d); } public static Vector2 operator /(Vector2 a, double d) { return new Vector2(a.X / d, a.Y / d); } public static implicit operator Point(Vector2 a) { return new Point((int)a.X, (int)a.Y); } public Vector2 UnitVector { get { return this / Length; } } public double Length { get { double aSq = Math.Pow(X, 2); double bSq = Math.Pow(Y, 2); return Math.Sqrt(aSq + bSq); } } public override string ToString() { return string.Format("[{0}, {1}]", X, Y); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload