I have a triangle solver, I want a way to use the values I get from the answer to draw a triangle to the screen that matches it.
Asked
Active
Viewed 3.0k times
31
-
By "triangle solver" I presume you mean something where you tell it two sides of a triangle and it calculates the third? Is it a right triangle? – Schwern Jul 14 '11 at 17:45
-
Kinda, I have a way for the user to enter any combination of information about any triangle. Such as two side and an angle, three sides, two angles and one side. And all the right triangle ones also. – user804306 Jul 29 '11 at 15:01
3 Answers
85
If you subclass a UIView you can implement something like this in drawRect to draw a triangle:
-(void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextBeginPath(ctx);
CGContextMoveToPoint (ctx, CGRectGetMinX(rect), CGRectGetMinY(rect)); // top left
CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect), CGRectGetMidY(rect)); // mid right
CGContextAddLineToPoint(ctx, CGRectGetMinX(rect), CGRectGetMaxY(rect)); // bottom left
CGContextClosePath(ctx);
CGContextSetRGBFillColor(ctx, 1, 1, 0, 1);
CGContextFillPath(ctx);
}

progrmr
- 75,956
- 16
- 112
- 147
-
I'm still a little confused on how to do this. Sorry I'm still fairly new to programming. – user804306 Jul 29 '11 at 15:02
-
1Create an objective-C class that is a subclass of UIView. Implement the drawRect: method in the class, iOS will call drawRect: when the view needs to be drawn on the screen. Instantiate an object of that class and add it to the view of your view controller. If that doesn't make sense then maybe you should get some of the basics from [this question](http://stackoverflow.com/questions/3315577/good-iphone-programming-books). – progrmr Jul 31 '11 at 11:32
-
Ok I understand the add Objective-c class, but what would I call when the users presses a button to draw the custom triangle to the screen? – user804306 Aug 13 '11 at 04:20
-
1You need to add the triangle view object to the view controller using addSubview, something like [myViewcontroller.view addSubView:triangleView]; The drawRect is called automatically by iOS at the right times. – progrmr Aug 13 '11 at 15:08
-
I also did some really interesting things to get a proper scale. I divided my longest triangle side by %85 of the number of points in the width then multiplied the other legs by that number to get any triangle to fit perfectly. – user804306 Jun 07 '12 at 21:06
-
2
Swift 3 equivalent for progrmr
's answer:
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else { return }
context.beginPath()
context.move(to: CGPoint(x: rect.minX, y: rect.minY))
context.addLine(to: CGPoint(x: rect.maxX, y: rect.midY))
context.addLine(to: CGPoint(x: (rect.minX), y: rect.maxY))
context.closePath()
context.setFillColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
context.fillPath()
}

Santosh
- 2,900
- 1
- 16
- 16
-1
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);
// Draw a triangle
CGContextSetRGBFillColor(ctx, 255, 160, 122, 1);
CGContextBeginPath(ctx);
CGContextMoveToPoint (ctx, 290, 35); // top
CGContextAddLineToPoint(ctx, 350, 165); // right
CGContextAddLineToPoint(ctx, 230,165); // left
CGContextClosePath(ctx);
CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
CGContextFillPath(ctx);
}
-
-
How does this answer add any new value to the question? It looks like it's just programr's answer with hard-coded values. Also it's just pure code, without a single line of explanation. – Lazar Ljubenović Mar 13 '17 at 14:01
-
Excuse me, Im a beginner and just wanted to show my way to draw a triangle. And yes, this is my code from one of projects. May be it will help to anyone else... – Anna Korr Mar 16 '17 at 17:18