gpt4 book ai didi

c# - 旋转曲线(一系列数据点)X 度定义了它们的新基线

转载 作者:行者123 更新时间:2023-11-30 23:12:23 28 4
gpt4 key购买 nike

我正在使用 MSchart 将 serie 绘制到其中,我需要将曲线(数据点系列)旋转 X 度,P1 是第一次点击(mouseDown),P2 是第二次点击(mouseUp),P1 和 P2 之间的角度代表角度旋转(我有这个计算),问题是当我应用不同的方法来做这个时曲线变形

for (int x = 1; x < curve.nPoints; x++)
{
double X0 = curve.get_array_X[x];
double Y0 = curve.get_array_Y[x];

System.Windows.Media.Matrix m = new System.Windows.Media.Matrix();
m.Rotate(45 * (Math.PI / 180.0));
System.Windows.Vector v = new System.Windows.Vector(X0, Y0);
v = System.Windows.Vector.Multiply(v, m);
sAus.Points.AddXY(v.X, v.Y);
}

和其他代码

public Series nueva(float X, float Y)
{
Series sAus = new Series(curvaActual.Tag);
int nearest1 = curvaActual.FindNearestXCV(new DataPoint(chartWorking1.ChartAreas[0].AxisX.PixelPositionToValue(X),
chartWorking1.ChartAreas[0].AxisY.PixelPositionToValue(Y)).XValue,
new DataPoint(chartWorking1.ChartAreas[0].AxisX.PixelPositionToValue(X), chartWorking1.ChartAreas[0].AxisY.PixelPositionToValue(Y)).YValues[0], 0, curvaActual.nPoints);
int nearest2 = curvaActual.FindNearestXCV(new DataPoint(chartWorking1.ChartAreas[0].AxisX.PixelPositionToValue(X),
chartWorking1.ChartAreas[0].AxisY.PixelPositionToValue(Y)).XValue,
new DataPoint(chartWorking1.ChartAreas[0].AxisX.PixelPositionToValue(X), chartWorking1.ChartAreas[0].AxisY.PixelPositionToValue(Y)).YValues[0], 0, curvaActual.nPoints);

double x1 = (double)curvaActual.get_array_X[nearest1];
double y1 = (double)curvaActual.get_array_Y[nearest1];


double x2 = (double)curvaActual.get_array_X[nearest2];
double y2 = (double)curvaActual.get_array_Y[nearest2];
double pendiente = Math.Atan2(y2 - y1, x2 - x1);
double anguloF = pendiente;
double deg = Math.PI * 45 / 180.0;
double coseno = Math.Cos(deg);
double seno = Math.Sin(deg);

double[] arrayX = new double[curvaActual.nPoints];
double[] arrayY = new double[curvaActual.nPoints];

for (int x = 1; x < curvaActual.nPoints; x++)
{

PointF rotatedPoint = RotatePoint(new PointF((float)curvaActual.get_array_X[x], (float)curvaActual.get_array_Y[x]), new PointF((float)curvaActual.get_array_X[x - 1], (float)curvaActual.get_array_Y[x - 1]), 45);
double angleRotatedPoint = angleFromPoint(rotatedPoint, new PointF((float)curvaActual.get_array_X[x - 1], (float)curvaActual.get_array_Y[x - 1]));


PointF pointROtado = RotatePoint(new PointF((float)curvaActual.get_array_X[x], (float)curvaActual.get_array_Y[x]), new PointF((float)0, (float)0), 45);
sAus.Points.AddXY((double)pointROtado.X, (double)pointROtado.Y);

}

return sAus;
}

[enter image description here]

[证明]

原创系列 enter image description here

旋转33

enter image description here

使用你的代码(将point修改为pointF)

void rotateSeries(Series src, Series tgt, DataPoint center, float angle)
{

PointF c = new PointF((float)center.XValue, (float)center.YValues[0]);

tgt.Points.Clear();
foreach (DataPoint dp in src.Points)
{
PointF p0 = new PointF((float)dp.XValue, (float)dp.YValues[0]);
PointF p = RotatePoint(p0, c, angle);
tgt.Points.AddXY(p.X, p.Y);
}
}
static PointF RotatePoint(PointF pointToRotate, PointF centerPoint, double angleInDegrees)
{
double angleInRadians = angleInDegrees * (Math.PI / 180);
double cosTheta = Math.Cos(angleInRadians);
double sinTheta = Math.Sin(angleInRadians);
return new PointF
{
X = (float)
(cosTheta * (pointToRotate.X - centerPoint.X) -
sinTheta * (pointToRotate.Y - centerPoint.Y) + centerPoint.X),
Y = (float)
(sinTheta * (pointToRotate.X - centerPoint.X) +
cosTheta * (pointToRotate.Y - centerPoint.Y) + centerPoint.Y)
};
}

最佳答案

这是您如何做到这一点的一个示例。它..

  • 假设您已找到要围绕其旋转图形的 DataPoint 中心
  • 假设您已经计算出它应旋转的角度
  • 使用两个 Series s1s2,第一个是源,后者是旋转的目标。
  • 使用 RotatePoint 的版本来自 Fraser 的 anwser,针对 PointF 稍作更新。

这是围绕第 5 个点旋转 33 度的结果:

enter image description here

private void button1_Click(object sender, EventArgs e)
{
rotateSeries(s1, s2, s1.Points[4], 33);
}

void rotateSeries(Series src, Series tgt, DataPoint center, float angle)
{

PointF c = new PointF((float)center.XValue, (float)center.YValues[0]);

tgt.Points.Clear();
foreach (DataPoint dp in src.Points)
{
PointF p0 = new Point((float)dp.XValue, (float)dp.YValues[0]);
PointF p = RotatePoint(p0, c, angle);
tgt.Points.AddXY(p.X, p.Y);
}
}

static PointF RotatePoint(PointF pointToRotate, PointF centerPoint, double angleInDegrees)
{
double angleInRadians = angleInDegrees * (Math.PI / 180);
double cosTheta = Math.Cos(angleInRadians);
double sinTheta = Math.Sin(angleInRadians);
return new PointF
{
X = (float)
(cosTheta * (pointToRotate.X - centerPoint.X) -
sinTheta * (pointToRotate.Y - centerPoint.Y) + centerPoint.X),
Y = (float)
(sinTheta * (pointToRotate.X - centerPoint.X) +
cosTheta * (pointToRotate.Y - centerPoint.Y) + centerPoint.Y)
};
}

如果您想使用 MouseClick 来确定角度,您可以直接使用 e 参数中的 Points。但要找出中心,您需要将像素坐标转换为图表坐标。

这是您可以在 MouseClick 事件期间使用的调用:

DataPoint clickedValuePoint(ChartArea ca, Point pt)
{
return new DataPoint(ca.AxisX.PixelPositionToValue(pt.X),
ca.AxisY.PixelPositionToValue(pt.Y));
}

现在让我们看看它是如何工作的,使用 DataPoint:

DataPoint centerPoint = null;

private void chart1_MouseClick(object sender, MouseEventArgs e)
{
ChartArea ca = chart1.ChartAreas[0];
centerPoint = clickedValuePoint(ca, e.Location);

rotateSeries(s1, s2, centerPoint, 33);
}

enter image description here

关于c# - 旋转曲线(一系列数据点)X 度定义了它们的新基线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44175963/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com