我正在以编程方式创建一个椭圆形状,但我找不到任何指定其位置的属性。Lines
有X1、Y1、X2、Y2,但椭圆形状上没有Center、Position、X、Y等。我该怎么做呢?
发布于 2011-04-08 21:07:57
在屏幕上的任意位置放置形状可能应该在Canvas Panel中完成(参见@phoog的响应)。但是如果你把它放在一个网格或其他面板中,你可以随时修改边距属性,把它放在你想要的地方。
如果要通过指定中心点而不是椭圆的左上角来执行此操作,可以执行以下操作:
Ellipse CreateEllipse(double width, double height, double desiredCenterX, double desiredCenterY)
{
Ellipse ellipse = new Ellipse { Width = width, Height = height };
double left = desiredCenterX - (width / 2);
double top = desiredCenterY - (height/ 2);
ellipse.Margin = new Thickness(left, top, 0, 0);
return ellipse;
}
我还没有检查它是否能在编译器中实现您想要的功能,但希望您能明白这一点。同样,与在非画布面板中使用边距相比,使用画布将是首选的方法,但计算左侧和顶部的相同原则仍然适用:
Canvas.SetLeft(ellipse, desiredCenterX - (width/2))
Canvas.SetTop(ellipse, desiredCenterY - (height/2))
发布于 2011-04-08 08:57:40
Canvas.Left和Canvas.Top。这一切都在“如何画椭圆或圆”http://msdn.microsoft.com/en-us/library/ms751563.aspx文档中。
在C#代码中,语法是这样的:
void CreateCanvasWithEllipse(double desiredLeft, double desiredTop)
{
Canvas canvas = new Canvas();
Ellipse ellipse = SomeEllipseConstructionMethod();
Canvas.SetLeft(ellipse, desiredLeft);
Canvas.SetTop(ellipse, desiredTop);
}
发布于 2013-03-15 11:11:24
如果您有起点和终点以及半径和布尔值,则使用我的函数:)
function ellipse(x1, y1, x2, y2, radius, clockwise) {
var cBx = (x1 + x2) / 2; //get point between xy1 and xy2
var cBy = (y1 + y2) / 2;
var aB = Math.atan2(y1 - y2, x1 - x2); //get angle to bulge point in radians
if (clockwise) { aB += (90 * (Math.PI / 180)); }
else { aB -= (90 * (Math.PI / 180)); }
var op_side = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)) / 2;
var adj_side = Math.sqrt(Math.pow(radius, 2) - Math.pow(op_side, 2));
if (isNaN(adj_side)) {
adj_side = Math.sqrt(Math.pow(op_side, 2) - Math.pow(radius, 2));
}
var Cx = cBx + (adj_side * Math.cos(aB));
var Cy = cBy + (adj_side * Math.sin(aB));
var startA = Math.atan2(y1 - Cy, x1 - Cx); //get start/end angles in radians
var endA = Math.atan2(y2 - Cy, x2 - Cx);
var mid = (startA + endA) / 2;
var Mx = Cx + (radius * Math.cos(mid));
var My = Cy + (radius * Math.sin(mid));
context.arc(Cx, Cy, radius, startA, endA, clockwise);
}
https://stackoverflow.com/questions/5589256
复制相似问题