如何调用Pie函数?

时间:2010-07-19 18:46:30

标签: delphi drawing

我有绘制饼图的中心点,半径和角度,但the Pie function需要4个点作为输入数据。有没有人有任何转换功能或更好的解释?

2 个答案:

答案 0 :(得分:2)

派对功能中的四点:

  1. 边界矩形的左上角。
  2. 边界矩形的右下角。
  3. 指向标记饼图开头的圆圈。
  4. 指向标记饼图末端的圆圈(逆时针)。
  5. 转换:

    中心点:Cx,Cy 半径:r 角度:a

    假设你的馅饼从顶部开始。

    1. X1 = Cx-r,Y1 = Cx + r
    2. X2 = Cx + r,Y2 = Cy-r
    3. X3 = Cx,Y3 = Y1
    4. X4 = Cx + r sin(a),Y4 = Cy + r cos(a)
    5. 你可能不得不在某个地方翻转一个标志,但这应该可以解决问题。

      有两个不同的天使(a和b):

      1. X3 = Cx + r sin(a),Y3 = Cy + r cos(a)
      2. X4 = Cx + r sin(b),Y4 = Cy + r cos(b)

答案 1 :(得分:0)

这是用(旧)C ++编写的,但大多数应该很容易转换为Delphi(或几乎其他任何东西)。它还假设输入是百分比(整圆是100%)而不是原始角度,但(再次)应该很容易处理。它具有从百分比到角度的弧度转换,因此从其他单位转换应该是非常简单的调整。

class section {
    double percent;
    int color;
public:

    section(double percent_, int color_) :
        percent(percent_), color(color_) {}

    void draw(HDC destination, POINT const &center, int diameter, double &start_angle);
};

void section::draw(HDC destination, POINT const &center, int radius, double &start_angle) {

    double start_x, start_y, end_x, end_y;
    double angle, end_angle;

    int top = center.y - radius;
    int bottom = center.y + radius;
    int left = center.x - radius;
    int right = center.x + radius;

    // now we have to convert a percentage to an angle in radians.
    // there are 100 percent in a circle, and 2*PI radians in a
    // circle, so we figure this percentage of 2*PI radians.
    angle = percent / 100.0 * 2.0 * 3.1416;

    end_angle = start_angle + angle;

    // Now we have to convert these angles into rectangular
    // coordinates in the window, which depend on where we're
    // putting the chart, and how big we're making it.
    start_x = center.x + radius * cos(start_angle);
    start_y = center.y + radius * sin(start_angle);

    end_x = center.x + radius * cos(end_angle);
    end_y = center.y + radius * sin(end_angle);

    // Now we need to actually draw the pie section by selecting
    // the correct color into the DC, drawing the section, then
    // selecting the original brush back, and deleing our brush.
    HBRUSH brush = CreateSolidBrush(color);

    HBRUSH old_brush = (HBRUSH)SelectObject(destination, brush);

    Pie(destination, left, top, right, bottom, 
        (int)start_x, (int)start_y, (int)end_x, (int)end_y);

    SelectObject(destination, old_brush);
    DeleteObject(brush);

    // our sole awareness of other sections: the next section will
    // start wherever we finished.
    start_angle = end_angle;
}