91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用WPF實現一個平面三角形3D運動效果

發布時間:2020-11-05 16:57:15 來源:億速云 閱讀:477 作者:Leah 欄目:開發技術

如何使用WPF實現一個平面三角形3D運動效果?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

實現效果如下:

如何使用WPF實現一個平面三角形3D運動效果

思路:封裝三角形三個頂點和路徑的三角形類,圖形渲染時同步更新公共頂點三角形的頂點位置。

步驟:

1、三角形類Triangle.cs

public Point A, B, C;//初始三個頂點
public Point VA, VB, VC;//運動的三個頂點
public Path trianglePath;//三角形路徑
public Color triangleColor;//填充
public double ColorIndex;//顏色深度
 
    public Triangle(Point a, Point b, Point c, Color co, double z)
    {
      A = VA = a;
      B = VB = b;
      C = VC = c;
      triangleColor = co;
      ColorIndex = z;
      trianglePath = new Path();
      Draw();
    }
 
    /// <summary>
    /// 繪制三角形
    /// </summary>
    public void Draw()
    {
      var g = new StreamGeometry();
      using (StreamGeometryContext context = g.Open())
      {
        context.BeginFigure(VA, true, true);
        context.LineTo(VB, true, false);
        context.LineTo(VC, true, false);
      }
      trianglePath.Data = g;
      trianglePath.Fill = new SolidColorBrush(triangleColor);
    }

2、 三角形系統類TriangleSystem.cs

public class TriangleSystem
  {
    /// <summary>
    /// 三角形列表
    /// </summary>
    private List<Triangle> triangles;
 
    /// <summary>
    /// 點和與其對應三角形字典
    /// </summary>
    public Dictionary<Point, PointClass> pointTriangles;
 
    /// <summary>
    /// 容器
    /// </summary>
    private Canvas containerCanvas;
 
    /// <summary>
    /// 三角形寬
    /// </summary>
    private int triangleWidth = 100;
 
    /// <summary>
    /// 三角形高
    /// </summary>
    private int triangleHeight = 100;
 
    /// <summary>
    /// 三角形橫向數量
    /// </summary>
    private int horizontalCount = 10;
 
    /// <summary>
    /// 三角形縱向數量
    /// </summary>
    private int verticalCount = 5;
 
    /// <summary>
    /// X坐標運動范圍
    /// </summary>
    private int XRange = 100;
 
    /// <summary>
    /// Y坐標運動范圍
    /// </summary>
    private int YRange = 10;
 
    /// <summary>
    /// 坐標運動速度
    /// </summary>
    private int speed = 10;
 
    /// <summary>
    /// 三角形顏色深度
    /// </summary>
    private double zIndex = 10.0;
 
    /// <summary>
    /// 隨機數
    /// </summary>
    private Random random;
 
    public TriangleSystem(Canvas ca)
    {
      containerCanvas = ca;
      random = new Random();
      triangles = new List<Triangle>();
      pointTriangles = new Dictionary<Point, PointClass>();
 
      SpawnTriangle();
    }
 
    /// <summary>
    /// 三角形初始化
    /// </summary>
    private void SpawnTriangle()
    {
      //清空隊列
      triangles.Clear();
 
      for (int i = 0; i < horizontalCount; i++)
      {
        for (int j = 0; j < verticalCount; j++)
        {
          Point A = new Point(i * triangleWidth, j * triangleHeight);
          Point B = new Point(i * triangleWidth, (j + 1) * triangleHeight);
          Point C = new Point((i + 1) * triangleWidth, (j + 1) * triangleHeight);
          Point D = new Point((i + 1) * triangleWidth, j * triangleHeight);
 
          double index = (i * horizontalCount / zIndex + j * verticalCount / zIndex ) / zIndex;
          index = index > 1 &#63; 1 : index < 0.1 &#63; 0.1 : index;
          Triangle t1 = new Triangle(A, B, C, GetTriangleColor(index), index);
          Triangle t2 = new Triangle(A, D, C, GetTriangleColor(index - 0.1), index - 0.1);
 
          //公共點和三角形集合鍵值對
          AddPointTriangles(A, t1, t2);
          AddPointTriangles(B, t1, t2);
          AddPointTriangles(C, t1, t2);
          AddPointTriangles(D, t1, t2);
 
          //添加三角形
          this.containerCanvas.Children.Add(t1.trianglePath);
          this.containerCanvas.Children.Add(t2.trianglePath);
          this.triangles.Add(t1);
          this.triangles.Add(t2);
        }
      }
    }
 
    /// <summary>
    /// 添加公共點和三角形集合鍵值對
    /// </summary>
    private void AddPointTriangles(Point p, Triangle t1, Triangle t2)
    {
      if (!this.pointTriangles.Keys.Contains(p))
      {
        List<Triangle> ts = new List<Triangle>();
        ts.Add(t1);
        ts.Add(t2);
        PointClass pc = new PointClass
        {
          triangles = ts,
          vector = new Vector(random.Next(-speed, speed) * 0.05, random.Next(-speed, speed) * 0.05),
        };
        this.pointTriangles.Add(p, pc);
      }
      else
      {
        if (!this.pointTriangles[p].triangles.Contains(t1))
          this.pointTriangles[p].triangles.Add(t1);
        if (!this.pointTriangles[p].triangles.Contains(t2))
          this.pointTriangles[p].triangles.Add(t2);
      }
    }
 
    /// <summary>
    /// 獲取三角形顏色
    /// </summary>
    private Color GetTriangleColor(double index)
    {
      return Color.FromArgb((byte)(255 * index), 230, 18, 65);
    }
 
    /// <summary>
    /// 更新三角形
    /// </summary>
    public void Update()
    {
      foreach (var pt in pointTriangles)
      {
        foreach (var t in pt.Value.triangles)
        {
          if (t.A == pt.Key)
            t.VA = GetPointValue(t.VA, t.A, ref pt.Value.vector, ref t.triangleColor, ref t.ColorIndex);
          if (t.B == pt.Key)
            t.VB = GetPointValue(t.VB, t.B, ref pt.Value.vector, ref t.triangleColor, ref t.ColorIndex);
          if (t.C == pt.Key)
            t.VC = GetPointValue(t.VC, t.C, ref pt.Value.vector, ref t.triangleColor, ref t.ColorIndex);
          t.Draw();
        }
      }
    }
 
    /// <summary>
    /// 計算頂點值
    /// </summary>
    private Point GetPointValue(Point p1, Point p2, ref Vector v, ref Color c, ref double index)
    {
      Point getPoint = new Point();
      if (p1.X + v.X < p2.X + XRange && p1.X + v.X > p2.X - XRange)
        getPoint.X = p1.X + v.X;
      else
      {
        v.X = -v.X;
        index = index > 1 &#63; index - 0.01 : index < 0.01 &#63; index + 0.01 : index - 0.01;
        c = GetTriangleColor(index);
        getPoint.X = p1.X + v.X;
      }
        
      if (p1.Y + v.Y < p2.Y + YRange && p1.Y + v.Y > p2.Y - YRange)
        getPoint.Y = p1.Y + v.Y;
      else
      {
        v.Y = -v.Y;
        getPoint.Y = p1.Y + v.Y;
      }
      return getPoint;
    }
  }

3、PointClass.cs

public class PointClass
  {
    public List<Triangle> triangles;
    public Vector vector;
  }

4、主窗體交互邏輯

private TriangleSystem ts;
 
 public MainWindow()
    {
      InitializeComponent();
      ts = new TriangleSystem(this.mainCanvas);
      CompositionTarget.Rendering += CompositionTarget_Rendering;
    }
 
    /// <summary>
    /// 幀渲染事件
    /// </summary>
    private void CompositionTarget_Rendering(object sender, EventArgs e)
    {
      ts.Update();
    }

不足:其中顏色渲染方式不夠完善,無法完全模仿3D起伏的效果

關于如何使用WPF實現一個平面三角形3D運動效果問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

wpf
AI

古浪县| 琼海市| 清新县| 安康市| 中牟县| 新密市| 油尖旺区| 辽阳市| 咸丰县| 安宁市| 江安县| 彰化县| 绥德县| 亚东县| 诸城市| 那坡县| 肥东县| 石屏县| 永福县| 张家口市| 五家渠市| 祁门县| 岳池县| 阿图什市| 衢州市| 咸阳市| 盐山县| 寿光市| 砚山县| 凌海市| 东宁县| 云梦县| 汉寿县| 临桂县| 镇赉县| 四平市| 兴隆县| 古交市| 静海县| 千阳县| 常德市|