简体   繁体   English

用鼠标C#Windows窗体图片框绘图

[英]C# Windows Form Picture Box Drawing With Mouse

EDIT: Drawing a 4 pointed star does work now with this code but i don't really know WHY this works, AND if i divide by the same number for x & y it just gives me a diamond??? 编辑:绘制一个4号尖的星星现在确实可以使用此代码,但我真的不知道为什么这样做,并且,如果我用相同的数字除以x和y,它只会给我一个菱形? 3 & 7 seem to be the best values too and i have no idea why... 3&7似乎也是最好的价值,我不知道为什么...

  public AP4Star() { }

    public AP4Star(int x1, int y1, int x2, int y2, Color c, bool solid, float penW) : base(x1, y1, x2, y2, c, solid, penW) { }

    public override void Draw(Graphics g)
    {
        float xDisplacement = Math.Abs(getX1() - getX2());
        float yDisplacement = Math.Abs(getY1() - getY2());

        PointF top = new PointF((getX1() + getX2()) / 2, Math.Min(getY2(), getY1()));
        PointF bottom = new PointF(top.X, Math.Max(getY2(), getY1()));
        PointF left = new PointF(Math.Min(getX2(), getX1()), (top.Y + bottom.Y) / 2);
        PointF right = new PointF(Math.Max(getX2(), getX1()), left.Y);

        PointF mtr = new PointF(right.X - xDisplacement / 3, right.Y - yDisplacement / 7);
        PointF mbr = new PointF(right.X - xDisplacement / 3, right.Y + yDisplacement / 7);
        PointF mbl = new PointF(left.X + xDisplacement / 3, left.Y + yDisplacement / 7);
        PointF mtl = new PointF(left.X + xDisplacement / 3, left.Y - yDisplacement / 7);





        PointF[] fourStar = { top,mtr, right, mbr, bottom, mbl, left, mtl };

        g.DrawPolygon(new Pen(getColor(), getPenWidth()), fourStar);

That code produce a pretty good pointy star but i feel like i am still doing this wrong... : result 该代码产生了一个很好的尖角星,但我感觉我仍然在做这个错误...: 结果

I don't think this is really a coding question, it's more of a logic question. 我不认为这确实是一个编码问题,而更多是逻辑问题。 But here's how I'd solve it: 但是这是我要解决的方法:

Start by zero-indexing all of your points. 首先对所有点进行零索引。 Assuming all of your points are equidistant from zero, that means n = 10 gives you four points like the following for your initial diamond: 假设所有点都与零等距,这意味着n = 10可以为您的初始菱形提供四个点,如下所示:

p1: { x = 0, y = 10}
p2: { x = 10, y = 0}
p3: { x = 0, y = -10}
p4: { x = -10, y = 0}

Now just add each of those points with a new point that has n / 4 (if it was n / 2, it'd be a straight line. So n / 4 ... or anything greater than 2, should get you a pointy star). 现在,只需将这些点中的每一个与n / 4的新点相加即可(如果为n / 2,那将是一条直线。因此n / 4 ...或大于2的任何点都应使您变得尖一点星)。 So if we use n/4, you get the following eight points: 因此,如果我们使用n / 4,则会得到以下八点:

p1: { x = 0, y = 10}
p2: { x = 2.5, y = 2.5}
p3: { x = 10, y = 0}
p4: { x = 2.5, y = -2.5}
p5: { x = 0, y = -10}
p6: { x = -2.5, y = -2.5
p7: { x = -10, y = 0}
p8: { x = -2.5, y = 2.5}

Now just draw a line between each of those points and you should have your pointy star. 现在只要在这些点之间画一条线,就应该有尖的星星。 I hope that's helpful! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM