Tutorial :Draw triangle iphone



Question:

How can i draw triangle in iphone..... Is there any api for this ? can you post some code...


Solution:1

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);  }  


Solution:2

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


Solution:3

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »