Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

More fun with Kernel language.



Everyone,

As another step on my road to generating regular polygons in a CoreImage kernel, I hunted up some code on the net to compute the distance from a point to a line, and I now have the following kernel:

kernel vec4 drawLine(sampler image, float thickness, float Ax, float Ay, float Bx, float By)
{
vec2 where = samplerCoord(image);
vec4 result = sample(image,where);
vec2 A, B, P;


// set up the endpoints
A.x = Ax;
A.y = Ay;
B.x =Bx;
B.y =By;
A *= samplerSize(image);
B *= samplerSize(image);

  // Compute where the minimum is between A and B
float q = ((where.x-A.x)*(B.x-A.x) + (where.y-A.y)*(B.y-A.y)) /
        (pow((B.x-A.x),2.0) + pow((B.y-A.y),2.0));

  // Clamp the result between zero and one
q = q < 0.0 ? 0.0 : q;
q = q > 1.0 ? 1.0 : q;

  // Compute the coordinates of the nearest point on the line segment
P.x = (1.0 - q)*A.x + q*B.x;
P.y = (1.0 -q)*A.y + q*B.y;

  // Set Alpha to zero if we're outside the desired distance.
result.a *= distance(P,where) < thickness ? 1.0 : 0.0;

return result;
}


which generates line segments with nice, rounded end-caps:

PNG image


I'm finding that my math skills were far rustier than I realized, so if anyone would care to take a pass at optimizing this code, I'm all ears. ;-) Also, I'd like to anti-alias the result. If anyone could offer a suggestion of how to do so, I'd certainly appreciate it.


Here's the composition:

Attachment: Circle kernel.qtz
Description: application/quartzcomposer


-jcr

John C. Randolph <email@hidden> (408) 914-0013
Roaming Cocoa Engineer,
Available for your projects at great Expense and Inconvenience.



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Quartzcomposer-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/quartzcomposer-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.