如何在 Silverlight 网格上放置椭圆?
我正在创建一个 silverlight 应用程序,它允许您单击屏幕上的两个位置,并绘制一个椭圆,其主轴在单击位置开始和结束。 可单击区域是 Silverlight 网格控件。 当前:
当您第一次单击时,我是:
- 在单击时放置标记 观点。
- 创建一个椭圆并为其设置父子关系 到网格。
- 创建和设置 椭圆上的角度变换。
当您移动鼠标时,我正在:
- 计算到 第一个点击点。
- 将椭圆的宽度设置为 这个长度。
- 计算一条线的角度 单击点和网格的 X 轴。
- 将椭圆的 AngleTransform 角度设置为此角度。
到目前为止,一切都很好。 显示椭圆,其长度和旋转角度随着鼠标移动而变化。
但是,椭圆的长轴偏离了单击点。 如何定位椭圆,使其长轴从单击点开始,到当前鼠标位置结束?
I am creating a silverlight application which will allow you to click at two places on the screen and will draw an ellipse whose major axis starts and ends at the click locations. The clickable area is a Silverlight Grid control. Currently:
When you first click, I am:
- Dropping a marker at the click
point. - Creating an ellipse and parenting it
to the Grid. - Creating and setting an
AngleTransform on the ellipse.
As you move the mouse, I am:
- Calculating the distance to the
first click point. - Setting the Width of the ellipse to
this length. - Calculating the angle of a line to
the click point and the Grid's
X-Axis. - Setting the Ellipse's AngleTransform Angle to this angle.
So far, so good. The ellipse is displayed, and its length and angle of rotation follow the mouse as it moves.
However, the major axis of the ellipse is offset from the click point.
How do I position the Ellipse so its major axis starts at the click point and ends at the current mouse position?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案是:
不要使用
System.Windows.Shapes.Ellipse
。 请改用 System.Windows.Shapes.Path 并在其中嵌入 EllipseGeometry。还将
Path.RenderTransform
设置为RotateTransform
。请勿在
路径
上设置宽度
、高度
或拉伸
。 而是设置EllipseGeometry
的Center
、RadiusX
和RadiusY
。最后,将
RotateTransform.Angle
设置为Ellipse
长轴和 X 轴的交叉角度(ArcTan
的长轴斜率)。 还将RotateTransform.CenterX
和CenterY
设置为EllipseGeometry
Center
。The answer turned out to be:
Don't use
System.Windows.Shapes.Ellipse
. Instead useSystem.Windows.Shapes.Path
and embed anEllipseGeometry
in it.Also set the
Path.RenderTransform
to aRotateTransform
.Don't set
Width
orHeight
orStretch
on thePath
. Instead set theCenter
,RadiusX
, andRadiusY
of theEllipseGeometry
.Finally, set the
RotateTransform.Angle
to the angle of intersection of theEllipse
major-axis and the X-axis (theArcTan
of the major-axis slope). Also setRotateTransform.CenterX
andCenterY
to theEllipseGeometry
Center
.如果我不得不猜测(代码会有所帮助),我认为您可以从开始单击点的差异添加到网格左侧的填充,这应该有助于将其移动偏移量。
If I had to guess (code would help), I would think that you could add padding from the difference of the beginning click point to the left side of the grid, which should help move it over by the offset.
对于您的应用程序来说,使用 Canvas 而不是网格可能是一个好主意,这样您就可以直接设置形状坐标。
May be it's a good idea to use Canvas instead of grid for your application, than you will be able to set up shapes coordianates directly.