只要你的部分磁盤平行於屏幕,並用平行投影渲染,最簡單的方法就是在不涉及OpenGL的情況下進行數學運算。
說你是繪畫的部分磁盤中:
glTranslatef(xPos, yPos, 0.0f);
gluPartialDisk(quadric, innerRad, outerRad, slices, loops, startAng, sweepAng);
現在,如果你想測試點(x0, y0)
,你減去平移矢量,然後計算出極座標:
x0 -= xPos;
y0 -= yPos;
float dist = sqrt(xPos * xPos + yPos * yPos);
float ang = atan2(yPos, xPos);
要在部分磁盤內部,到中心的距離必須在半徑範圍內:
if (dist < innerRad || dist > outerRad) {
// it's outside!
}
角度稍微複雜一點,因爲它環繞。而且,atan2()
結果爲以弧度爲單位測得的在一個範圍[-PI,PI],而參數gluPartialDisk()
是在度x軸逆時針,和從Y軸順時針測量。具有範圍startAng
和sweepAng
[0.0,360.0]度時,間隔測試邏輯可能看起來像這樣(未測試):
ang *= 180.0f/PI; // convert to degrees
ang = 90.0f - ang; // make clockwise, relative to y-axis
if (ang < 0.0f) {
ang += 360.0f; // wrap into range [0.0, 360.0]
}
ang -= startAng; // make relative to startAng
if (ang < 0.0f) {
ang += 360.0f; // ... and back into range [0.0, 360.0]
}
if (ang > sweepAng) {
// it's outside!
} else {
// it's inside!
}