請提前原諒英語不好。
你好!我目前正在使用 QGraphicsView 和 QGraphicsItem 實作視圖小部件。
有沒有辦法只在一個圓圈內繪制網格線?
矩形很好,但試圖將它們畫在一個圓圈內是一種痛苦。
下面是示例代碼。
for(int x = rect.left(); x <= rect.right(); x) {
painter->drawLine(QPointF(x, rect.top()), QPointF(x, rect.bottom()));
}
for(int y = rect.top(); y <= rect.bottom(); y) {
painter->drawLine(QPointF(rect.left(), y), QPointF(rect.right(), y));
}
這是我當前的狀態->當前網格線結果
結果我想要的看起來像-> Reuslt 示例我想要的
我不想顯示超出圓圈范圍的網格線。
如果您有什么好的想法,請回復。
謝謝你!
uj5u.com熱心網友回復:
這只是 tigronometry 計算的問題。從 X 或 Y 軸上的位置,您可以使用反正弦和反余弦計算到圓上點的角度。下面的代碼應該可以作業
#include <math.h>
class Point
{
public:
double X = 0.0;
double Y = 0.0;
};
int main()
{
double radius = 10.0;
double stepx = 2.0;
double stepy = 2.0;
int stepsInX = radius / stepx;
int stepsInY = radius / stepy;
// this is just for positive X (need another loop for negative X)
for (int i = 0; i <= stepsInX; i )
{
double angle = acos(i*stepx / radius);
double y = radius * sin(angle);
Point p1;
p1.X = i * stepx;
p1.Y = y;
Point p2;
p2.X = i * stepx;
p2.Y = -y;
drawLine(p1, p2);
}
// this is just for positive Y (need another loop for negative Y)
for (int i = 0; i <= stepsInY; i )
{
double angle = asin(i * stepy / radius);
double x = radius * cos(angle);
Point p1;
p1.X = x;
p1.Y = i * stepy;
Point p2;
p2.X = -x;
p2.Y = i * stepy;
drawLine(p1, p2);
}
}
void drawLine(Point const& p1, Point const& p2)
{
// your code to draw line here
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/482818.html
