Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GCMC source code: Draw and label a decade of logarithmic ticks
//----
// Define tick layout for scales
// Numeric value for scale, corresponding tick length
TickScaleNarrow = {
[1.0,TickMajor],
[1.1,TickMinor],[1.2,TickMinor],[1.3,TickMinor],[1.4,TickMinor],
[1.5,TickMid],
[1.6,TickMinor],[1.7,TickMinor],[1.8,TickMinor],[1.9,TickMinor],
[2.0,TickMajor],
[2.2,TickMinor],[2.4,TickMinor],[2.6,TickMinor],[2.8,TickMinor],
[3.0,TickMajor],
[3.2,TickMinor],[3.4,TickMinor],[3.6,TickMinor],[3.8,TickMinor],
[4.0,TickMajor],
[4.5,TickMinor],
[5.0,TickMajor],
[5.5,TickMinor],
[6.0,TickMajor],
[6.5,TickMinor],
[7.0,TickMajor],
[7.5,TickMinor],
[8.0,TickMajor],
[8.5,TickMinor],
[9.0,TickMajor],
[9.5,TickMinor]
};
TickScaleWide = {
[1.0,TickMajor],
[1.1,TickMinor],[1.2,TickMinor],[1.3,TickMinor],[1.4,TickMinor],
[1.5,TickMid],
[1.6,TickMinor],[1.7,TickMinor],[1.8,TickMinor],[1.9,TickMinor],
[2.0,TickMajor],
[2.1,TickMinor],[2.2,TickMinor],[2.3,TickMinor],[2.4,TickMinor],
[2.5,TickMid],
[2.6,TickMinor],[2.7,TickMinor],[2.8,TickMinor],[2.9,TickMinor],
[3.0,TickMajor],
[3.2,TickMinor],[3.4,TickMinor],[3.6,TickMinor],[3.8,TickMinor],
[4.0,TickMajor],
[4.2,TickMinor],[4.4,TickMinor],[4.6,TickMinor],[4.8,TickMinor],
[5.0,TickMajor],
[5.5,TickMinor],
[6.0,TickMajor],
[6.5,TickMinor],
[7.0,TickMajor],
[7.5,TickMinor],
[8.0,TickMajor],
[8.5,TickMinor],
[9.0,TickMajor],
[9.5,TickMinor]
};
TickLabels = [1,2,5]; // labels only these ticks, must be integers
/-----
// Draw a decade of ticks & labels
// ArcLength > 0 = CCW, < 0 = CW
// UnitOnly forces just the unit tick, so as to allow creating the last tick of the scale
function DrawTicks(Radius,TickMarks,TickOrient,UnitAngle,ArcLength,Decade,LabelOrient,UnitOnly) {
feedrate(ScaleSpeed);
local a,r0,r1,p0,p1;
if (Decade == 1 || UnitOnly) { // draw unit marker
a = UnitAngle;
r0 = Radius + TickOrient * (TickMajor + 2*TickGap + ScaleTextSize.y);
p0 = r0 * [cos(a),sin(a)];
r1 = Radius + TickOrient * (ScaleSpace - 2*TickGap);
p1 = r1 * [cos(a),sin(a)];
goto(p0);
move([-,-,EngraveZ]);
move(p1);
goto([-,-,TravelZ]);
}
local ticklist = UnitOnly ? {TickMarks[0]} : TickMarks;
local tick;
foreach(ticklist; tick) {
a = UnitAngle + ArcLength * log10(tick[0]);
p0 = Radius * [cos(a), sin(a)];
p1 = (Radius + TickOrient*tick[1]) * [cos(a), sin(a)];
goto(p0);
move([-,-,EngraveZ]);
move(p1);
goto([-,-,TravelZ]);
}
feedrate(TextSpeed); // draw scale values
local lrad = Radius + TickOrient * (TickMajor + TickGap);
if (TickOrient == INWARD) {
if (LabelOrient == INWARD) {
lrad -= ScaleTextSize.y; // inward ticks + inward labels = offset inward
}
}
else {
if (LabelOrient == OUTWARD) {
lrad += ScaleTextSize.y; // outward ticks + outward labels = offset outward
}
}
ticklist = UnitOnly ? [TickLabels[0]] : TickLabels;
local ltext,lpath,tpa;
foreach(ticklist; tick) {
ltext = to_string(Decade * to_int(tick));
lpath = scale(typeset(ltext,TextFont),ScaleTextSize);
a = UnitAngle + ArcLength * log10(tick);
tpa = ArcText(lpath,[0mm,0mm],lrad,a,TEXT_CENTERED,LabelOrient);
engrave(tpa,TravelZ,EngraveZ);
}
}
@ednisley

This comment has been minimized.

Copy link
Owner Author

ednisley commented Nov 28, 2019

More details on my blog at https://wp.me/poZKh-8Bz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.