English 中文(简体)
在 WPF 中绘制支持多个角度的三角形
原标题:Drawing a triangle in WPF that supports multiple angles

我在 WPF 中绘制三角形时使用 < code> GeophicDrawing 来绘制。 目前我能够将它绑定在与滑动器相连的视图模式 s “ 角度” 属性上, 该属性是用户可以移动的, 从而在对象周围移动矩形 。 问题是我要让矩形能够根据基于缩放值的特定角度, 使矩形更宽或更窄。 目前我无法更改矩形, 因为我不知道如何在 < code> GeophicalDrawing 对象上这样做 。 也许应该使用另一个对象?

几何绘图对象代码是:

<GeometryDrawing Geometry="M100,100 L186.6,280 A100,100,0,0,1,13.4,280 L100,100">
     <GeometryDrawing.Brush>
         <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" Opacity="0.25">
               <GradientStopCollection>
                     <GradientStop Color="Black" Offset="0" />
                     <GradientStop Color="Transparent" Offset="0.9"/>
               </GradientStopCollection>
         </LinearGradientBrush>
     </GeometryDrawing.Brush>
</GeometryDrawing>

应用程序的 UI 是这个( 只是一个测试项目, 我用它来测试控制程序, 然后再在真实项目中执行) 。

"https://i.sstatic.net/zIx8B.png" alt="矩形问题的UI。淡化矩形是问题所在"/"

谢谢你的帮助 朋友们!

约翰

问题回答

您可以用两个线段和“http://msdn.microsoft.com/en-us/library/system.windows.media.arcsegment.aspx' rel=“nofollow” >ArcSection 取代目前的几何绘图字符串。

<ArcSegment Size="100,50" 
            IsLargeArc="True" 
            SweepDirection="CounterClockwise" 
            Point="200,100" />

此外,一个弧对一个视野领域来说比三角更自然,特别是当角度大(接近180度)时。

<强 > EDIT

这比它看起来要难得多, 因为您需要计算弧的终点。 我看不到其它解决方案, 只能计算代码中的终点 。

好吧,我设法把弧打开和关闭。我这样做的方式是给弧两条线下定义像这样的弧线

<PathGeometry>
      <PathFigure StartPoint="50,0" IsClosed="True">
             <LineSegment Point="0,100" x:Name="m_leftLine" />
             <LineSegment Point="100,100" x:Name="m_rightLine" />
      </PathFigure>
</PathGeometry>

然后为幻灯片 s ValueCheanged 事件在后面写一个代码, 并使用所需的角度重新计算行 X 位置。 这产生了以下代码 :

public partial class MyFovControl : UserControl
{
private float m_oldAngleValue;
private float m_newAngleValue;

public MyFovControl()
{
  InitializeComponent();
  this.zoomSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(zoomSlider_ValueChanged);
  m_oldAngleValue = m_newAngleValue = 0;
}

void zoomSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
  m_newAngleValue = (float)(Convert.ToDouble((double)lblFovXAngle.Content));

  // Happens only once the first time.
  if (m_oldAngleValue == 0)
  {
    m_oldAngleValue = m_newAngleValue;
  }

  m_leftLine.Point = new Point(m_leftLine.Point.X + (m_oldAngleValue - m_newAngleValue), m_leftLine.Point.Y);
  m_rightLine.Point = new Point(m_rightLine.Point.X - (m_oldAngleValue - m_newAngleValue), m_rightLine.Point.Y);
  m_oldAngleValue = m_newAngleValue;
  }
}

我知道,非常美食, 但这是我唯一能想到的方法, 从我上网搜索到的东西-- 可能是唯一的办法。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签