Writing Name Using Bezier Curves In C#

為{幸葍}努か 提交于 2020-01-06 16:44:08

问题


I have to make a program that uses C# Generated Graphics to make a replica of my name that I wrote in cursive. Twist is, I have to use Bezier Curves. I've already called a function to make Bezier Curves using 4 points and a gravity concept. My question to you is, What would be the easiest way to make around 10 curves.

Here is my function for a Bezier Curve.

public static void bezierCurve(
     Graphics g, 
     double p1x, double p1y, 
     double p2x, double p2y, 
     double p3x, double p3y, 
     double p4x, double p4y)
{
    double t, r1x, r4x, r1y, r4y;
    float x, y;

    Pen black = new Pen(Color.Black);

    r1x = 3 * (p2x - p1x);
    r4x = 3 * (p4x - p3x);

    r1y = 3 * (p2y - p1y);
    r4y = 3 * (p4y - p3y);

    t = 0;
    while (t <= 1)
    {
        x = (float) ((2 * Math.Pow(t, 3) - 3 * Math.Pow(t, 2) + 1) * p1x
            + (-2 * Math.Pow(t, 3) + 3 * Math.Pow(t, 2)) * p4x
            + (Math.Pow(t, 3) - 2 * Math.Pow(t, 2) + t) * r1x
            + (Math.Pow(t, 3) - Math.Pow(t, 2)) * r4x);
        y = (float) ((2 * Math.Pow(t, 3) - 3 * Math.Pow(t, 2) + 1) * p1y
            + (-2 * Math.Pow(t, 3) + 3 * Math.Pow(t, 2)) * p1y
            + (Math.Pow(t, 3) - 2 * Math.Pow(t, 2) + t) * r1y
            + (Math.Pow(t, 3) - Math.Pow(t, 2)) * r4y);

        g.DrawRectangle(black, x, y, 1, 1);

        t = t + 0.01;
    }
}

回答1:


I would suggest taking some vector editing software, e.g. InkScape or Corel, draw your name with beziers using that software, then save as .SVG. The SVG format is easy to understand, here is an example of encoding a bezier path. Copy the coordinates from the path into your program. Alternatively, use a piece of graph paper to get the coordinates by hand.

C# already has a function for drawing Beziers, see Graphics.DrawBezier, that is going to be much more efficient (and producing better-looking results) than your implementation.



来源:https://stackoverflow.com/questions/1539636/writing-name-using-bezier-curves-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!