首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >两个关节的DrawEllipse (点,更确切地说是X和Y坐标)

两个关节的DrawEllipse (点,更确切地说是X和Y坐标)
EN

Stack Overflow用户
提问于 2014-11-13 14:39:56
回答 1查看 1.2K关注 0票数 1

我想用椭圆而不是线来显示骨骼。我有两个点,与X和Y的坐标。当我想画一个椭圆时,我需要

代码语言:javascript
复制
public abstract void DrawEllipse(
Brush brush,
Pen pen,
Point center,
double radiusX,
double radiusY

)

因此,我尝试了这段代码,但是有一些错误(不知道radiusY):

代码语言:javascript
复制
 double centerX = (jointPoints[jointType0].X + jointPoints[jointType1].X) / 2;
        double centerY = (jointPoints[jointType0].Y + jointPoints[jointType1].Y) / 2;
        double radiusX =Math.Sqrt( (Math.Pow((jointPoints[jointType1].X - jointPoints[jointType0].X), 2)) + (Math.Pow((jointPoints[jointType1].Y - jointPoints[jointType0].Y), 2)));
        drawingContext.DrawEllipse(null, drawPen, new Point(centerX, centerY), radiusX, radiusX/5);

有谁可以帮我?

代码语言:javascript
复制
private void DrawBone(IReadOnlyDictionary<JointType, Joint> joints, IDictionary<JointType, Point> jointPoints, JointType jointType0, JointType jointType1, DrawingContext drawingContext, Pen drawingPen,List<JointType> badJoint)
    {
        Joint joint0 = joints[jointType0];
        Joint joint1 = joints[jointType1];

        // If we can't find either of these joints, exit
        if (joint0.TrackingState == TrackingState.NotTracked ||
            joint1.TrackingState == TrackingState.NotTracked)
        {
            return;
        }



        // We assume all drawn bones are inferred unless BOTH joints are tracked
        Pen drawPen = this.inferredBonePen;

        if ((joint0.TrackingState == TrackingState.Tracked) && (joint1.TrackingState == TrackingState.Tracked))
        {
            drawPen = drawingPen;
        }
        //If a bone makes parts of an one bad angle respect reference angle
        if (badJoint.Contains(jointType0) && badJoint.Contains(jointType0))
            drawPen = new Pen(Brushes.Red, 6);
        drawingContext.DrawLine(drawPen, jointPoints[jointType0], jointPoints[jointType1]);
EN

回答 1

Stack Overflow用户

发布于 2014-11-14 08:36:57

您不能(仅仅)使用DrawEllipse方法,因为这将始终绘制水平或垂直的省略。

使用此代码实现旋转:https://stackoverflow.com/a/5298921/1974021,并编写接受以下输入参数的方法:

  1. Focalpoint1
  2. FocalPoint2
  3. 半径

椭圆既可以用焦点来描述,也可以用(组合)半径来描述。如果你使用的焦点,省略将重叠在关节,以创建一个圆形的模式在每个关节。是因为你想要什么吗?(如果你只想让它们在关节处触碰,就更容易了)

好吧,其实这不是焦点,而是交汇圈的中心。试试这个方法:

代码语言:javascript
复制
private static void DrawEllipse(Pen pen, Graphics g, PointF pointA, PointF pointB, float radius)
{
    var center = new PointF((pointA.X + pointB.X) / 2, (pointA.Y + pointB.Y) / 2);
    var distance = GetDistance(pointA, pointB);

    // The axis are calculated so that the center of the osculating circles are conincident with the points and has the given radius.
    var a = radius + distance / 2; // Semi-major axis
    var b = (float)Math.Sqrt(radius * a); // Semi-minor axis


    // Angle in degrees
    float angle = (float)(Math.Atan2(pointA.Y - pointB.Y, pointA.X - pointB.X) * 180 / Math.PI);
    using (Matrix rotate = new Matrix())
    {
        GraphicsContainer container = g.BeginContainer();
        rotate.RotateAt(angle, center);
        g.Transform = rotate;
        g.DrawEllipse(pen, center.X-a, center.Y-b, 2 * a, 2 * b);
        g.EndContainer(container);
    }
}

private static float GetDistance(PointF a, PointF b)
{
    var dx = a.X - b.X;
    var dy = a.Y - b.Y;
    return (float)Math.Sqrt(dx * dx + dy * dy);
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26911416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档