如何计算弧的中心点?

时间:2013-08-18 19:15:53

标签: math canvas graphics drawing android-canvas

我有一个由几个饼图组成的CustomPieView。我必须在每个馅饼部分的中间画一些东西。

// Inside Activity的onCreate方法

public void onCreate(Bundle savedInstanceState){

    int size = 400;
    int bgColor = 0xffa11b1;
    ViewPieChart piechart = (ViewPieChart) findViewById(R.id.pieChartView);
    piechart.setCallBack(this);
    piechart.setLayoutParams(new LayoutParams(size, size));
    piechart.setGeometry(size, size, 2, 2, 2, 2, 2130837504);
    piechart.setSkinparams(bgColor);
    piechart.setData(piedata, maxCount);
    piechart.invalidate();

}

// CustomPieView扩展了视图

public void setGeometry(int width, int height, int gapleft, int gapright, int gaptop, int gapbottom, int overlayid) {

    mWidth = width;
    mHeight = height;
    mGapleft = gapleft;
    mGapright = gapright;
    mGapBottm = gapbottom;
    mGapTop = gaptop;
}

protected void onDraw(Canvas canvas){

    canvas.drawColor(Color.DKGRAY);
    mBagpaints.setAntiAlias(true);
    mBagpaints.setStyle(Paint.Style.FILL);
    mBagpaints.setColor(0x88FF0000);
    mBagpaints.setStrokeWidth(0.0f);
    mLinePaints.setAntiAlias(true);
    mLinePaints.setColor(Color.WHITE);
    mLinePaints.setStrokeWidth(3.0f);
    mLinePaints.setStyle(Paint.Style.STROKE);       
    sLinePaint.setAntiAlias(true);
    sLinePaint.setColor(Color.BLACK);
    sLinePaint.setStrokeWidth(3.0f);
    sLinePaint.setStyle(Paint.Style.STROKE);

    RectF mOvals = new RectF(mGapleft, mGapTop, mWidth - mGapright, mHeight
            - mGapBottm);
    mStart = START_INC;
    PieDetailsItem item;
    for (int i = 0; i < mdataArray.size(); i++) {
        item = (PieDetailsItem) mdataArray.get(i);
        mBagpaints.setColor(item.color);
        mSweep = (float) 360* ((float) item.count / (float) mMaxConnection);
        canvas.drawArc(mOvals, mStart, mSweep, true, mBagpaints);
        canvas.drawArc(mOvals, mStart, mSweep, true, mLinePaints);

//以下功能是设置全局变量             cSegX,cSegY到该段的中心。不工作!!

        calculateMidPointOfPieSegment(mSweep);
        canvas.drawPoint(cSegX, cSegY, sLinePaint);
        mStart = mStart + mSweep;
    }
    mState = IS_DRAW;
    callBack.onDrawFinished(null);
}

private float calculateRadius(){
float width = mWidth/2;
float height = mHeight/2;
    if(width < height){
        return width;
    }else{
        return height;
    }
}

private void calculateMidPointOfPieSegment(float sweepAngle){
    cSegX = (float)((calculateRadius()/2)*Math.cos(Math.toRadians(sweepAngle/2))+(mWidth/2));
    cSegY = (float)((calculateRadius()/2)*Math.sin(Math.toRadians(sweepAngle/2))+(mHeight/2));
}

只需看到下图中的点,它就必须位于每个弧的中心。 Screenshot

Ref.

完成Source

1 个答案:

答案 0 :(得分:0)

如果清除它,请告诉我。这是您要实现的基本逻辑。

    //rimPoint = (rX, rY)
    //centerPoint = (cX,cY)
    //theta is the angle of the the midpoint measured anticlockwise from the x axis, 
    //or the average of the two angles making up the slice(measured anticlockwise from 
    //the x axis)
    private void calculateMidPointOfPieSegment(cX,cY, theta)
    {
       float rX = cX + calculateRadius()*(cos(theta))
       float rY = cY + calculateRadius()*(sin(theta))

       cSegX = (rX+cX)/2;
       cCegY = (rY+cY)/2;
    }

如果theta不是弧度,则将其分解为theta * Math.PI / 180