C# itext 扇形 弧形

 PdfContentByte cb = writer.DirectContent; 

//第一二個參數代表起始點的XY座標,三四參數代表拉伸點的座標,五六參數代表終止點的座標

cb.Arc(350f, 70f, 550f, 130f, 270f, 90f);

cb.SetLineDash(3f, 3f);

cb.Fill();

 

//以下使用貝塞爾曲線畫扇形,思路在圓內構建正多邊形,確定多邊形的頂點

int r = 200;
            PointF p0 = new PointF(300, 590);
            PointF p1 = getPointF(p0, r, 0);

            List<PointF> pointList = getPolygonPoint(15, p0, p1);

            double xdiff = p1.X - pointList[0].X;
            double ydiff = p1.Y - pointList[0].Y; ;
            double distance = Math.Sqrt(xdiff * xdiff + ydiff * ydiff);
            float newR = getWR(r, 15, distance);


            PointF p3 = getPointF(p0, newR, 0);
            PointF p2 = getPointF(p0, newR, 0);


            List<PointF> pointList1 = getPolygonPoint(15, p0, p3);
            List<PointF> pointList2 = getPolygonPoint(30, p0, p2);


            //cb.MoveTo(p0.X, p0.Y);
            //cb.CurveFromTo(p0.X, p0.Y, p1.X, p1.Y);
            //cb.CurveTo(p1.X, p1.Y, pointList[0].X, pointList[0].Y, pointList[1].X, pointList[1].Y);
            //cb.FillStroke();

            cb.SetColorFill(BaseColor.GREEN);
            cb.MoveTo(p2.X, p2.Y);
            foreach (PointF p in pointList2)
            {
                cb.LineTo(p.X, p.Y);
            }

            cb.Fill();

            cb.SetColorFill(BaseColor.YELLOW);
            cb.MoveTo(p3.X, p3.Y);
            foreach (PointF p in pointList1)
            {
                cb.LineTo(p.X, p.Y);
            }

            cb.Fill();


            cb.Circle(p0.X, p0.Y, 3);
            cb.Fill();

            cb.Circle(p1.X, p1.Y, 6);
            cb.Fill();


            cb.Circle(pointList[0].X, pointList[0].Y, 9);
            cb.Fill();

            cb.Circle(pointList[1].X, pointList[1].Y, 12);
            cb.Fill();

            cb.SetColorFill(BaseColor.GRAY);
            cb.MoveTo(p1.X, p1.Y);
            foreach (PointF p in pointList)
            {
                cb.LineTo(p.X, p.Y);
            }

            cb.Fill();

            cb.SetColorStroke(BaseColor.RED);
            cb.SetColorFill(BaseColor.RED);
            cb.MoveTo(p0.X, p0.Y);
            cb.CurveFromTo(p0.X, p0.Y, p1.X, p1.Y);
            cb.CurveTo(p1.X, p1.Y, pointList2[0].X, pointList2[0].Y, pointList[0].X, pointList[0].Y);
            cb.FillStroke();

            cb.Circle(p0.X, p0.Y, r);
            cb.Stroke();

            cb.SetColorStroke(BaseColor.BLACK);
            cb.Circle(p0.X, p0.Y, newR);
            cb.Stroke();

//得到新圓的半徑

private float getWR(float r, int n, double a)
        {

            double b = a * Math.Sin(360 / 2 / n);
            double newR = Math.Sqrt(Math.Pow(b, 2) + Math.Pow(r, 2));
           // double newR = r / Math.Cos(360 / n / 2);
            return (float)newR;
        }

 

//根據圓心,半徑,角度數得到圓上的一點
        private PointF getPointF(PointF p0, float r, int angle = 45)
        {
            float Y = p0.Y + (float)(r * Math.Sin(angle * Math.PI / 180));
            float X = p0.X + (float)(r * Math.Cos(angle * Math.PI / 180));
            return new PointF(X, Y);
        }

//根據邊數,圓心,以及圓上一點得到正多邊形的其他頂點
        private List<PointF> getPolygonPoint(int polygon, PointF p0, PointF p1)
        {

            Matrix rotation = new Matrix();
            rotation.RotateAt(360 / polygon, new System.Drawing.PointF(p0.X, p0.Y));//60爲角度,如12邊行爲30

            PointF[] pointf = new PointF[] { p1 };
            List<PointF> lst = new List<PointF>();
            for (int i = 0; i < polygon - 1; i++)
            {
                rotation.TransformPoints(pointf);
                lst.Add(pointf[0]);
            }
            return lst;
        }

圖形使用貝塞爾曲線所話  ,以下爲主要代碼

cb.SetColorStroke(BaseColor.RED);
            cb.SetColorFill(BaseColor.RED);
            cb.MoveTo(p0.X, p0.Y);
            cb.CurveFromTo(p0.X, p0.Y, p1.X, p1.Y);
            cb.CurveTo(p1.X, p1.Y, pointList2[0].X, pointList2[0].Y, pointList[0].X, pointList[0].Y);
            cb.FillStroke();

以上代碼爲網上查找資料,自己整理,希望能幫助到更多同樣需求的同伴

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章