开发者

Draw triangle iphone

How can i draw triangle in iph开发者_高级运维one..... Is there any api for this ? can you post some code...


You should take a look at QuartzDemo from Apple Documentation, which have a lot of examples of drawings.

Using this demo, you can change de star (6 edges) to a triangle (3 edges) in the Polygon demo. Just change the number of incrementation in the loop of QuartzPolygons.m, just bellow the comment "// Add a star to the current path". For example, here is the code for an equilateral triangle :

-(void)drawInContext:(CGContextRef)context
{
    // Drawing with a white stroke color
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
    // Drawing with a blue fill color
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
    // Draw them with a 2.0 stroke width so they are a bit more visible.
    CGContextSetLineWidth(context, 2.0);

    CGPoint center;

    // Add a triangle to the current path
    center = CGPointMake(90.0, 90.0);
    CGContextMoveToPoint(context, center.x, center.y + 60.0);
    for(int i = 1; i < 3; ++i)
    {
        CGFloat x = 60.0 * sinf(i * 4.0 * M_PI / 3.0);
        CGFloat y = 60.0 * cosf(i * 4.0 * M_PI / 3.0);
        CGContextAddLineToPoint(context, center.x + x, center.y + y);
    }
    // And close the subpath.
    CGContextClosePath(context);

    // Now draw the triangle with the current drawing mode.
    CGContextDrawPath(context, drawingMode);
}


Just draw three lines. For a triangle ABC you need to draw lines A-B, B-C and C-A.


you can try something like

Vertex3D    vertex1 = Vertex3DMake(0.0, 0.0, -3.0);
    Vertex3D    vertex2 = Vertex3DMake(0.1, 0.1, -3.0);
    Vertex3D    vertex3 = Vertex3DMake(0.1, -0.1, -3.0);
        Triangle3D  triangle = Triangle3DMake(vertex1, vertex2, vertex3);
    glClearColor(0.4, 0.4, 0.4, 1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glEnableClientState(GL_VERTEX_ARRAY);
        glColor4f(1.0, 0.0, 0.0, 1.0);
        glVertexPointer(3, GL_FLOAT, 0, &triangle);
        glDrawArrays(GL_TRIANGLES, 0, 9);

but before this you need to initiate OpenGL

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜