Alternatives for LineTo() ?

Posted on 1997-03-25
Last Modified: 2013-12-26
I am the author of an After Dark freeeware screensaver
module called 'The Swarm' which is a straightforward port of
the classic Unix 'xswarm' program. (You can go to for some
screenshots of the module, or for downloading it).

The bees in the swarm are simply coloured line segments.
Every frame of the animation, I draw the bees into a GWorld,
I calculate the enclosing rectangle of the entire swarm, and
I blit that onto the real screen using CopyBits (after
blacking out the previous frame's rectangle first).

This approach yields acceptable performance for
not-too-large swarms, even on 68k machines. However, what
has always been bugging me about my implementation from the
very beginning, is that if I profile the code it turns out
that in a frame of the animation the most time is *not*
spend on the two CopyBits calls (as I had been expecting),
but rather on the LineTo calls used to draw the individual
bee line segments.

I find it hard to believe that drawing twenty or thirty line
segments into a GWorld would be that performance-intensive
(and it is not the function call overhead either, I've
checked that).

So, can anybody out there explain to me *why* LineTo is so
computation-intensive, or better yet: suggest an alternative
approach for drawing lines that might yield better results?
Question by:Leo Breebaart

Accepted Solution

AdamS earned 100 total points
ID: 1291418
Using LineTo to draw the bees is like ..... is like......Im at loss for an analogy...

LineTo doesnt just put lines one the bitmap. It does other things. If you want to optimize your drawing  even more then write directly to the bitmap. You have the address of the bitmap.

You can use the following to write to B&W bitmaps:

void BitSet(Ptr bytePtr, long bitNum);
Boolean BitTst(Ptr bytePtr, long bitNum)

I used this for my application that put sampled sound onto a scope on the screen.

For color...its a bit more complicated but you can figure it out. Each pixel is the concatenation of the Red Green and Blue components (right aligned). Red is the most significant bits, blue is the least significant bits. Higher order bytes come first in the pixel data. The pixel data itself is stored in the baseAddr field of the PixMap.

Note, you can get all this information from the Quickdraw part of Think Reference.

Ok hows this for an analogy. Using LineTo to draw every piece of the bee is like breaking out a new roll toilet paper every time you want to wipe yourself.


Author Comment

by:Leo Breebaart
ID: 1291419
Adam's answer essentially boils down to: "implement your
own LineTo function which draws directly to the bitmap".
Fair enough, but I do think I would have appreciated a slightly more helpful answer than "for color its a bit more complicated but you can figure it out"...
Also, he didn't really answer the part of my question that
asked *why* LineTo is so slow. "It does other things", he says. Like WHAT??? Why doesn't Apple provide an optimized line-drawing
routine of their own? What's the bottleneck in LineTo? Won't I run into the same bottleneck if I naively start to implement
my own version?
With this in mind I have to say that although Adam's answer is useful as confirmation of what I already suspected, it's not *quite* as helpful an answer as I had been hoping for.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Turn a spreadsheet into a vba executable. 2 96
format the code in java 6 102
difference between String.subString() and String.subSequence() 6 241
fizzArray2 challenge 1 91
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question