ScratchData LogoScratchData
Back to gtoal's profile

Quad and Triangle Fill (v3) - library code

GTgtoal•Created March 9, 2015
Quad and Triangle Fill (v3) - library code
17
17
748 views
View on Scratch

Instructions

Arbitrary quad filling. Click on any vertex and move it around, while holding the mouse down. If it detects that the quad is axis aligned (in either X or Y) it uses an optimised version of the drawing procedure (you'll know when because you'll see the color change). This code is meant for use in other programs. Note that currently the code is sensitive to the ordering of the points - upper left, upper right, then lower left, lower right. That may change later to be more coder-friendly.

Description

TO DO: there's a minor glitch where the bases of abutting quads are drawn twice. This is usually OK but messes up with a transparent pen. Also I should add clip-rectangle support. See https://scratch.mit.edu/projects/118962685/ where both these features have been added - I need to retrofit the fixes to this original code. --- What's new in this release - everything is now ultimately drawn using the axis-aligned quad primitive. I would prefer to decompose arbitrary quads directly into axis-aligned quads for consistency, but that's not today's priority. For now, arbitrary quads are decomposed into two triangles, and then the triangles are drawn with the quad drawing code :-) There's definitely some work needing to be done to handle the ordering of the vertices and the concave/self-intersecting shapes that can occur with certain placements. I'm trying to avoid writing a "Graham Scan" to handle these. I'd rather work out a solution myself than go look one up... Finally... the axis-aligned filler uses floating point arithmetic to calculate the slopes of the sides. A better implementation (to follow) would be to use Bresenham's Line Algorithm instead of the simple interpolation here. After some experimentation, it appears that it's a *lot* easier to use a DDA than Bresenham's, because a DDA guarantees stepping by 1 in either X or Y as desired, whereas Bresenham's can only step by 1 for lines whose slope is in the correct octant...

Project Details

Project ID51488276
CreatedMarch 9, 2015
Last ModifiedAugust 3, 2017
SharedMarch 9, 2015
Visibilityvisible
CommentsAllowed

Remix Information

Parent ProjectView Parent
Root ProjectView Root