intepolacja dla Color, Punkt3D, Vector3D

0

Witam
Muszę zaimplementować dwie kilka klas do interfejsu, mam problem jak ugryźć interpolacje dla
System.Windows.Media.Color (tu chyba chodzi o RGB), System.Windows.Media.Media3D.Point3D i System.Windows.Media.Media3D.Vector3D

Mam zrobione np dla float:
(IInterpolator.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nazwa.Interpolators
{
    public interface IInterpolator
    {
        object Interpolate(object from, object to, double ratio);
    }
}

(FloatInterpolator.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nazwa.Interpolators
{
    public class DoubleInterpolator : IInterpolator
    {
        #region IInterpolator Members

        public object Interpolate(object from, object to, double ratio)
        {
            var fromD = (float)from;
            var toD = (float)to;

            return fromD + (toD - fromD) * ratio;
        }

        #endregion// IInterpolator Members
    }
}

Prosiłbym o wskazówki jak mam do tego podejść.
Z góry wielkie dzięki.

1

Hmm. A takie coś nie może być?

    public class ColorInterpolator : IInterpolator
    {
        #region IInterpolator Members

        public object Interpolate(object from, object to, double ratio)
        {
            var fromC = (Color)from;
            var toC = (Color)to;

            toC.R = (byte)(fromC.R + (toC.R - fromC.R) * ratio);
            toC.G = (byte)(fromC.G + (toC.G - fromC.G) * ratio);
            toC.B = (byte)(fromC.B + (toC.B - fromC.B) * ratio);

            return toC;
        }

        #endregion// IInterpolator Members
    }
0

Wielkie dzięki, jeszcze muszę dwa pozostałe zrobić:)

0
namespace Nazwa.Interpolators
{
    public interface IInterpolator
    {
        object Interpolate(object from, object to, double ratio);
    }

    public interface IInterpolator<T>: IInterpolator
    {
        T Interpolate(T from, T to, double ratio);
    }

    public class ColorInterpolator: IInterpolator<Color>
    {
        public Color Interpolate(Color from, Color to, double ratio)
        {
            return Color.Multiply(from, (float)(1 - ratio)) + Color.Multiply(to, (float)ratio);
        }

        public object Interpolate(object from, object to, double ratio)
        {
            return Interpolate((Color)from, (Color)to, ratio);
        }
    }

    public class Point3DInterpolator : IInterpolator<Point3D>
    {
        public Point3D Interpolate(Point3D from, Point3D to, double ratio)
        {
            double iRatio = 1 - ratio;
            return new Point3D(
                from.X * iRatio + to.X * ratio,
                from.Y * iRatio + to.Y * ratio,
                from.Z * iRatio + to.Z * ratio);
        }

        public object Interpolate(object from, object to, double ratio)
        {
            return Interpolate((Point3D)from, (Point3D)to, ratio);
        }
    }

    public class Vector3DInterpolator : IInterpolator<Vector3D>
    {
        public Vector3D Interpolate(Vector3D from, Vector3D to, double ratio)
        {
            double iRatio = 1 - ratio;
            return new Vector3D(
                from.X * iRatio + to.X * ratio,
                from.Y * iRatio + to.Y * ratio,
                from.Z * iRatio + to.Z * ratio);
        }

        public object Interpolate(object from, object to, double ratio)
        {
            return Interpolate((Vector3D)from, (Vector3D)to, ratio);
        }
    }
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1