提问人:Summit 提问时间:11/13/2023 最后编辑:Summit 更新时间:11/14/2023 访问量:57
如何从曲线中获取贝塞尔点
How to get Bezier points from a curve
问:
我正在使用内置的Qt函数在Qt中绘制贝塞尔曲线:
void OpacityCurveWidget::paintEvent(QPaintEvent* event) {
QPainter painter(this);
painter.fillRect(0, 0, width(), height(), Qt::white);
painter.setPen(Qt::blue);
QPainterPath path;
glm::vec2 startPoint(0, (1.0 - opacityCurve[0].opacity) * height()); // Invert y-coordinate
path.moveTo(startPoint.x, startPoint.y);
for (size_t i = 1; i < opacityCurve.size(); ++i) {
int x1 = static_cast<int>(opacityCurve[i - 1].position.x * width());
int x2 = static_cast<int>(opacityCurve[i].position.x * width());
int y1 = static_cast<int>((1.0 - opacityCurve[i - 1].position.y) * height()); // Invert y-coordinate
int y2 = static_cast<int>((1.0 - opacityCurve[i].position.y) * height()); // Invert y-coordinate
int cx1 = static_cast<int>(opacityCurve[i - 1].handle2.x * width());
int cy1 = static_cast<int>((1.0 - opacityCurve[i - 1].handle2.y) * height()); // Invert y-coordinate
int cx2 = static_cast<int>(opacityCurve[i].handle1.x * width());
int cy2 = static_cast<int>((1.0 - opacityCurve[i].handle1.y) * height()); // Invert y-coordinate
QPointF startPointF(startPoint.x, startPoint.y);
QPointF controlPoint1F(cx1, cy1);
QPointF controlPoint2F(cx2, cy2);
QPointF endPointF(x2, y2);
path.cubicTo(controlPoint1F, controlPoint2F, endPointF);
绘制它们后,我能够移动点,它们的手柄将新点和手柄 (x,y) 位置数据保存到矢量中。
根据这些数据,我尝试在给定的 x 位置处获得曲线 y 位置:
float OpacityCurveWidget::getYPositionAtX(float x) const {
if (opacityCurve.size() < 2) {
return 0.0f;
}
// Find the segment of the Bezier curve that contains the given x-coordinate
size_t segmentIndex = 0;
while (segmentIndex < opacityCurve.size() - 1 && x > opacityCurve[segmentIndex + 1].position.x) {
segmentIndex++;
}
// Calculate t, the parameter for the cubic Bezier curve formula
float t = (x - opacityCurve[segmentIndex].position.x) /
(opacityCurve[segmentIndex + 1].position.x - opacityCurve[segmentIndex].position.x);
// Use the cubic Bezier formula to calculate the y-coordinate
float y = (1 - t) * (1 - t) * (1 - t) * opacityCurve[segmentIndex].position.y +
3 * (1 - t) * (1 - t) * t * opacityCurve[segmentIndex].handle2.y +
3 * (1 - t) * t * t * opacityCurve[segmentIndex + 1].handle1.y +
t * t * t * opacityCurve[segmentIndex + 1].position.y;
// Invert the y-coordinate
y = 1.0f - y;
// Return the y-coordinate without multiplying by height
return y;
}
我面临的问题是,当我移动曲线点手柄的 x 位置时,这也会影响 Qt 图中曲线点的 y 位置,但在我的计算中,仅在 x 方向上移动任何手柄点不会影响点的 y 位置。
当我在 x 方向上移动任何手柄时,它也会影响曲线点的 y 位置。
当总 X 长度为 0.5 - 0 时,如何计算 X 1.0 处的 Y 位置?
答: 暂无答案
评论
y
x