Re: drawing pixels
Re: drawing pixels
- Subject: Re: drawing pixels
- From: "b.bum" <email@hidden>
- Date: Sun, 21 Mar 2004 09:12:44 -0800
Heh. I went through something similar about a year and a half ago when
I also wanted to draw plain, non-antialiased, 1x1, unscaled pixels very
quickly.
NSRectFillList() is your friend. An OpenGL implementation would
likely be faster, but this is universal and a heck of a lot easier
[imo].
Some sample code (from a PyObjC project -- if anyone wants the full
project, I'd be happy to send it):
The drawing code (I use an NSBitmapImageRep to cache the previous
iterations. Once rendered once, the rendering is captured into an
NSBIR):
def drawRect_(self, aRect):
if self.backingStore:
self.backingStore.draw()
else:
self.eraseView_(aRect)
for pointArray, rectCount, color in self.pointsCountsAndColors:
color.set()
NSRectFillList(pointArray, len(pointArray) / 4)
self.pointsCountsAndColors = []
self.backingStore =
NSBitmapImageRep.alloc().initWithFocusedViewRect_(self.bounds())
Where pointArray is filled by:
def hopalong(a=30.0, b=1.0, c=0.9, iterations=1000000, numColors=1000,
scale=10.0, xoffset=150, yoffset=150):
x = 0.0
y = 0.0
colors = genColors(numColors)
colorIter = iterations / len(colors)
while 1:
for i in range(len(colors)):
points = array.array('f', [0.0,0.0,1.0,1.0] * colorIter)
for j in range(colorIter):
if x < 0:
temp = y + math.sqrt(abs(b * x - c))
else:
temp = y - math.sqrt(abs(b * x - c))
y = a - x
x = temp
points[j*4] = xoffset + (scale * x)
points[(j*4) + 1] = yoffset + (scale * y)
yield (colors[i], points)
Specifically, this line:
points = array.array('f', [0.0,0.0,1.0,1.0] * colorIter)
Creates an array of floating point numbers with the pattern
[0,0,1,1,0,0,1,1,0,0,1,1]. colorIter is just the number of points
that should be rendered in the current color. Since NSRectFillList()
takes an array of NSRect structures as an NSRect*, you can effectively
just create a an array of floats and initialize 'em in linear, 4 float
chunks where each set of 4 floats is one rectangle.
In the above case, I create the array of 1x1 rects all located at 0,0
and then walk the array and set the x,y coord of each individual rect:
points[j*4] = xoffset + (scale * x)
points[(j*4) + 1] = yoffset + (scale * y)
Translation to ObjC is left as an exercise to the reader. Another
excercise, yield() is very cool. Very, very cool. The same can sort
of be done with enumerators, but not quite.
On Mar 21, 2004, at 4:26 AM, Tobias Kl|pfel wrote:
Hi List,
please help me - I am getting absolutely nowhere.
I want to draw pixels. You might not remember them, but we used to have
them in computer graphics. They are those small quadratic dots on your
screen. I know it sounds old-fashioned, but I really need to draw
those.
I have tried drawing one-by-one rectangles, but that is very slow.
Other avenues I tried were:
control the data object of an NSBitmapImageRep (I gave up trying to
figure out the format - there has to be an easier way)
OpenGL has points, or so I've been told. Again, I am convinced there
should be a better way.
I am trying to draw Lyapunov diagrams, which are similar to fractals.
Please tell me the best way to draw pixels, and please point me to some
sample code.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.