Exemple #1
0
    public void Test_S1ChordAngle_Trigonometry()
    {
        const int kIters = 20;

        for (int iter = 0; iter <= kIters; ++iter)
        {
            double       radians = Math.PI * iter / kIters;
            S1ChordAngle angle   = new(S1Angle.FromRadians(radians));
            Assert2.Near(Math.Sin(radians), angle.Sin(), S2.DoubleError);
            Assert2.Near(Math.Cos(radians), angle.Cos(), S2.DoubleError);
            // Since the tan(x) is unbounded near Pi/4, we map the result back to an
            // angle before comparing.  (The assertion is that the result is equal to
            // the tangent of a nearby angle.)
            Assert2.Near(Math.Atan(Math.Tan(radians)), Math.Atan(angle.Tan()), S2.DoubleError);
        }

        // Unlike S1Angle, S1ChordAngle can represent 90 and 180 degrees exactly.
        S1ChordAngle angle90  = S1ChordAngle.FromLength2(2);
        S1ChordAngle angle180 = S1ChordAngle.FromLength2(4);

        Assert.Equal(1, angle90.Sin());
        Assert.Equal(0, angle90.Cos());
        Assert.Equal(double.PositiveInfinity, angle90.Tan());
        Assert.Equal(0, angle180.Sin());
        Assert.Equal(-1, angle180.Cos());
        Assert.Equal(0, angle180.Tan());
    }
Exemple #2
0
    // Faster than the function above, but cannot accurately represent distances
    // near 180 degrees due to the limitations of S1ChordAngle.
    public static S2Point GetPointOnRay(S2Point origin, S2Point dir, S1ChordAngle r)
    {
        System.Diagnostics.Debug.Assert(origin.IsUnitLength());
        System.Diagnostics.Debug.Assert(dir.IsUnitLength());
        // The error bound below includes the error in computing the dot product.
        System.Diagnostics.Debug.Assert(origin.DotProd(dir) <=
                                        S2.kRobustCrossProdError + 0.75 * S2.DoubleEpsilon);

        // Mathematically the result should already be unit length, but we normalize
        // it anyway to ensure that the error is within acceptable bounds.
        // (Otherwise errors can build up when the result of one interpolation is
        // fed into another interpolation.)
        //
        // Note that it is much cheaper to compute the sine and cosine of an
        // S1ChordAngle than an S1Angle.
        return((r.Cos() * origin + r.Sin() * dir).Normalize());
    }