代码之家  ›  专栏  ›  技术社区  ›  jb.

C使用角度度和三角绘制多边形

  •  1
  • jb.  · 技术社区  · 15 年前

    我使用下面的方法来计算六边形的顶点,然后做一个简单的 graphics.DrawPolygon(pen, aptsVertices) 以后再画。

    protected override void CalculateVertices()
    {
        //member variables
        aptsVertices = new Point[6];
        deg = 120;
        rad = deg * (Math.PI / 180);
    
        double nSinDeg = Math.Sin(rad);
        double nCosDeg = Math.Cos(rad);
    
        aptsVertices[0] = ptFirstVertex;
    
        for(int i = 1; i < aptsVertices.Length; i++)
        {
            double x = aptsVertices[i - 1].X - nCosDeg * nSideLength;
            double y = aptsVertices[i - 1].Y - nSinDeg * nSideLength;
            aptsVertices[i] = new Point((int)x, (int)y);
    
            //recalculate the degree for the next vertex
            deg += 120;
            rad = deg * (Math.PI / 180);
    
            nSinDeg = Math.Sin(rad);
            nCosDeg = Math.Cos(rad);
        }
    }
    
    4 回复  |  直到 13 年前
        1
  •  3
  •   digEmAll    15 年前

    deg += 120 deg step = 60°

    public static Point[] CalculateVertices(int nSides, int nSideLength, Point ptFirstVertex)
    {
        if (nSides < 3)
            throw new ArgumentException("Polygons can't have less than 3 sides...");
    
        var aptsVertices = new Point[nSides];
        var deg = (180.0 * (nSides - 2)) / nSides;
        var step = 360.0 / nSides;
        var rad = deg * (Math.PI / 180);
    
        double nSinDeg = Math.Sin(rad);
        double nCosDeg = Math.Cos(rad);
    
        aptsVertices[0] = ptFirstVertex;
    
        for (int i = 1; i < aptsVertices.Length; i++)
        {
            double x = aptsVertices[i - 1].X - nCosDeg * nSideLength;
            double y = aptsVertices[i - 1].Y - nSinDeg * nSideLength;
            aptsVertices[i] = new Point((int)x, (int)y);
    
    
            //recalculate the degree for the next vertex
            deg -= step;
            rad = deg * (Math.PI / 180);
    
            nSinDeg = Math.Sin(rad);
            nCosDeg = Math.Cos(rad);
    
        }
        return aptsVertices;
    }
    




        2
  •  0
  •   sanjoyd    15 年前

    point [i] = new Point (center.x + sideLength * cos ((Math.PI * i) / 6), center.y + sideLength * sin ((Math.PI * i) / 6));

        3
  •  0
  •   jpreed00    14 年前

    public static PointD[] CalculateVertices(int nSides, int nSideLength, PointD ptFirstVertex)
    {
        //calculate the points for a polygon of N sides
        if (nSides < 3)
            throw new ArgumentException("Polygons can't have less than 3 sides...");
    
        var aptsVertices = new PointD[nSides];
        var deg = (180.0 * (nSides - 2)) / nSides;
        var step = 360.0 / nSides;
        var rad = deg * (Math.PI / 180);
    
        double nSinDeg = Math.Sin(rad);
        double nCosDeg = Math.Cos(rad);
    
        aptsVertices[0] = ptFirstVertex;
    
        for (int i = 1; i < aptsVertices.Length; i++)
        {
            double x = aptsVertices[i - 1].X - nCosDeg * nSideLength;
            double y = aptsVertices[i - 1].Y - nSinDeg * nSideLength;
            aptsVertices[i] = new PointD(x, y);
    
            //recalculate the degree for the next vertex
            deg -= step;
            rad = deg * (Math.PI / 180);
    
            nSinDeg = Math.Sin(rad);
            nCosDeg = Math.Cos(rad);
    
        }
        return aptsVertices;
    }
    

    public class PointD
    {
        public double X, Y;
        public PointD(double _x, double _y)
        {
            X = _x;
            Y = _y;
        }
    }
    
        4
  •  0
  •   coopersita    13 年前

    -(NSArray *) calculateVertices:(int)numSides center:(CGPoint)ctr{
    NSMutableArray *vertices = [[NSMutableArray alloc] init];
    
    if (numSides < 3) {
        return vertices;
    }
    
    float deg = ((180.0 * (numSides-2) / numSides)-180)/2;
    
    float step = 360.0 / numSides;
    float rad = deg * (M_PI / 180);
    
    double nSinDeg = sin(rad);
    double nCosDeg = cos(rad);
    
    int sl = [self calculateSideLength:numSides];
    
    CGPoint firstPoint = CGPointMake(ctr.x, ctr.y-radius);    
    [vertices addObject:[NSValue valueWithCGPoint:firstPoint]];
    
    for (int i = 1; i < numSides; i++) {
        NSValue *vp = [vertices objectAtIndex:i-1];
        CGPoint pp =[vp CGPointValue];
        double x = pp.x - nCosDeg * sl;
        double y = pp.y - nSinDeg * sl;
        CGPoint np = CGPointMake(x, y);
        [vertices addObject:[NSValue valueWithCGPoint:np]];
    
        deg -= step;
        rad = deg * (M_PI / 180);
    
        nSinDeg = sin(rad);
        nCosDeg = cos(rad);
    
    }
    
    return vertices;
    
    }
    
    -(int) calculateSideLength:(int)numSides{
       int length = (2*radius)*sin(M_PI/numSides);
       return length;
    }