♠ SWSpade Reference

Inverted Heart Curve — Parametric Class — SketchWaveJS

♠ Spade Mathematics

The Inverted Heart Formula

SWSpade draws the classic playing-card spade (♠) using the same parametric formula as SWHeart, but with the y-component negated. This flips the curve vertically: the sharp point moves to the top and the two rounded lobes fall to the bottom, giving the spade silhouette.

Parametric Equations (Local Space)

x(t) = scale · a · sin³(t)
y(t) = −scale · (b · cos(t) + c · cos(2t) + d · cos(3t) + e · cos(4t))

t ∈ [0, 2π),  200 sample points

The y-negation (compared to the heart) is the key transformation that creates the spade shape.

Default Coefficients & Geometry

With the default parameters (a=16, b=13, c=−5, d=−2, e=−1, scale=0.5):

Featuret valueLocal y (approx)Description
Sharp tip (top)π+8.5The characteristic spade point
Notch (center-bottom)0−2.5The V-notch between the two lobes
Lobe bottoms≈ 0.65, 5.63 rad≈ −5.3The lowest points; stem attaches here
Lobe half-width≈ π/2x ≈ ±4.0Widest extent of the body

Y-Negation: Heart → Spade

The original heart formula (SWHeart) places the rounded lobes at the top and the sharp point at the bottom. Negating y (multiplying the entire y-formula by −1) produces a vertical mirror image: now the sharp tip is at top and lobes are at bottom — the spade.

Stem Attachment

The stem is a filled trapezoid. Its attachment y-coordinate is found by iterating all 200 body sample points in local space and taking the minimum y (≈ −5.3 for defaults). This ensures the stem always connects flush to the bottom of the body regardless of parameter changes.

  • Neck width = stemWidth × 0.22
  • Foot width = stemWidth (wider base)
  • Height = stemLength (extends downward from attachment point)

Constructor

new SWSpade(center, a, b, c, d, e, fillColor, strokeColor, thickness, scale, stemLength, stemWidth, rotationDeg)
ParameterTypeDefaultDescription
centerSWPointrequiredCenter position in user (grid) coordinates
anumber16Body width coefficient; controls the horizontal extent of the lobes
bnumber13Primary y-coefficient; controls overall body height
cnumber−5Second harmonic coefficient; deepens the V-notch
dnumber−2Third harmonic coefficient; refines lobe shape
enumber−1Fourth harmonic coefficient; subtle shape adjustment
fillColorSWColorundefinedFill color; undefined = no fill
strokeColorSWColorundefinedStroke color; undefined = no stroke
thicknessnumber2Stroke weight in pixels
scalenumber0.5Global scale factor applied to both x and y body coordinates
stemLengthnumber2.0Height of the trapezoid stem (grid units)
stemWidthnumber5.5Foot width of the trapezoid stem (grid units); neck = stemWidth × 0.22
rotationDegnumber0Static base rotation (CCW degrees); preserved across reset()
// Minimal construction (all defaults)
const center = new SWPoint(0, 0, undefined, 8,
    new SWColor(240, 80, 80, 100));
const fill   = SWColor.fromHex('#22225a', 100, 'fill');
const stroke = SWColor.fromHex('#1a1a40', 100, 'stroke');
const spade  = new SWSpade(center, 16, 13, -5, -2, -1,
                             fill, stroke, 2, 0.5, 2.0, 5.5, 0);
spade.drawOnGrid(grid);

Properties

Live Properties

PropertyTypeDescription
centerSWPointCenter position. Drag to reposition; center.shouldShow controls dot visibility
a, b, c, d, enumberShape coefficients (see Math section)
scalenumberGlobal scale factor applied to body
stemLengthnumberStem trapezoid height in grid units
stemWidthnumberStem foot width in grid units
fillColorSWColorFill color (undefined = no fill)
strokeColorSWColorStroke color (undefined = no outline)
thicknessnumberStroke weight in pixels
rotationDegnumberStatic base rotation (CCW degrees)
rotationnumberAccumulated spin rotation (degrees); cleared by reset()

Static Property

PropertyValueDescription
SWSpade.SAMPLE_COUNT200Number of parametric sample points per full t ∈ [0, 2π) revolution

Methods

Drawing

MethodDescription
draw() Draws in raw pixel (screen) coordinates. Prefer drawOnGrid() for standard use.
drawOnGrid(grid) Draws body + stem mapped through the given SWGrid. Handles y-flip automatically. Use this in the p5.js draw loop.

Animation

MethodParametersDescription
rotate(deltaAngle) deltaAngle: degrees (CCW+, CW−) Accumulates spin rotation for body + stem together. Call each frame: spade.rotate(speed * deltaT)

Setters

MethodParameterDescription
setA(v)numberSets coefficient a (lobe width). Recalculates body.
setB(v)numberSets coefficient b (primary height).
setC(v)numberSets coefficient c (notch depth).
setD(v)numberSets coefficient d (lobe refinement).
setE(v)numberSets coefficient e (subtle adjustment).
setScale(s)number > 0Sets global scale. Use during Pulse animation.
setStemLength(l)number > 0Sets stem trapezoid height.
setStemWidth(w)number > 0Sets stem foot width. Neck = w × 0.22.
setRotation(deg)numberSets static base rotation (CCW degrees). Does not clear accumulated rotation.
setFillColor(fc)SWColorReplaces fill color (deep copy stored).
setStrokeColor(sc)SWColorReplaces stroke color (deep copy stored).
setStrokeWeight(w)numberSets stroke thickness in pixels.
setFillAlpha(alpha)0–100Updates fill opacity and rebuilds the p5 color object.
setStrokeAlpha(alpha)0–100Updates stroke opacity and rebuilds the p5 color object.

Reset & Utility

MethodDescription
reset() Restores a, b, c, d, e, scale, stemLength, stemWidth, colors, and thickness to constructor values. Clears accumulated spin rotation. Does not move center.
SWSpade.copy(other) (static) Returns a deep copy of other (including center SWPoint, colors, and accumulated rotation).

Code Examples

1. Basic Setup (p5.js global mode)

let grid, spade;

function setup() {
    createCanvas(400, 400);
    colorMode(HSB, 360, 100, 100, 100);
    initializeSWColors();

    grid = new SWGrid({ UL: new SWPoint(-10, 10), LR: new SWPoint(10, -10) });

    const center = new SWPoint(0, 0, undefined, 8,
        new SWColor(240, 80, 80, 100));
    const fill   = SWColor.fromHex('#22225a', 100, 'fill');
    const stroke = SWColor.fromHex('#1a1a40', 100, 'stroke');
    spade = new SWSpade(center, 16, 13, -5, -2, -1,
                         fill, stroke, 2, 0.5, 2.0, 5.5, 0);
}

function draw() {
    background(0, 0, 93);
    grid.draw();
    spade.drawOnGrid(grid);
    grid.updateScreenBounds();
}

2. Spin Animation

let prevT = 0;
const SPIN_SPEED = 45; // degrees per second (CCW)

function draw() {
    const t = millis() / 1000;
    const deltaT = (prevT > 0) ? (t - prevT) : 0;
    prevT = t;

    background(0, 0, 93);
    grid.draw();
    spade.rotate(SPIN_SPEED * deltaT);
    spade.drawOnGrid(grid);
    grid.updateScreenBounds();
}

3. Pulse Animation (Scale Oscillation)

const BASE_SCALE    = 0.5;
const PULSE_SPEED   = 0.5;   // Hz
const PULSE_AMOUNT  = 0.08;  // scale units

function draw() {
    const t = millis() / 1000;

    background(0, 0, 93);
    grid.draw();

    const s = BASE_SCALE + PULSE_AMOUNT * Math.sin(2 * Math.PI * PULSE_SPEED * t);
    spade.setScale(Math.max(0.05, s));

    spade.drawOnGrid(grid);
    grid.updateScreenBounds();
}

4. Ghostly Trail Effect

// In setup(): create a semi-transparent background color
// bgColor = new SWColor(0, 0, 93, 15, 'bg').col;  // 15% opacity background

// In draw():
background(bgColor);   // semi-transparent clear → old frames fade slowly
spade.rotate(2);        // fast spin creates a ghostly star trail
spade.drawOnGrid(grid);

Source Code

Show / Hide swSpade.js source
// Source will be loaded here — see shapeClasses/swSpade.js
↑ Top