Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

xcodeNewbie

macrumors member
Original poster
Jul 1, 2011
65
0
I'm trying to make an app in which the user enter an equation (i.e y=2x) and the iPhone will graph it on screen. I am wondering how to turn on individual pixels on the iPhone screen so I can draw the graph. At first I tried creating a whole bunch of rectangles with height and width of 1, but the program ran way too slowly trying to allocate and initialize all those rectangles. So I'm wondering if there's a simple command for turning on pixels, something like:
Code:
turnPixelOn:(UIView *)aView atCoordinates:(CGPoint)aPoint withColor:(UIColor*)aColor.
 
You cannot "turn a pixel on". You can easily create a bitmap image, get the raw data array that represents that image and with a bit of knowledge about the structure of the image (you can get this with low level CoreGraphics functions) you can manipulate the pixels directly.

Note this is a reasonably complex thing to do and I'm certainly not about to post code.
 
Drawing Pixels

OK, thanks. I read about the bitmapping and I was just trying to find an easier way to graph an equation. But if this is what it takes....
 
You can subclass UIView and implement your own drawRect method to make your graph or you can draw directly into a bitmap and present that bitmap as a view.

Either way you need to use CoreGraphics to perform the actual drawing. Check out the WWDC 2011 videos, session 129 "Practical Drawing for iOS Developers" shows how to draw a graph.
 
The first thing that I would try for graphing is drawing lines with a CGPathRef. I think that would be pretty fast and easy to do, probably less code than you have now with the 1x1 rectangles.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.