How to resolve the algorithm Pinstripe/Printer step by step in the Wren programming language
How to resolve the algorithm Pinstripe/Printer step by step in the Wren programming language
Table of Contents
Problem Statement
The task is to demonstrate the creation of a series of 1 point wide vertical pinstripes with a sufficient number of pinstripes to span the entire width of the printed page (except for the last pinstripe). The pinstripes should alternate one point white, one point black. (Where the printer does not support producing graphics in terms of points, pixels may be substituted in this task.) After the first inch of printing, we switch to a wider 2 point wide vertical pinstripe pattern. alternating two points white, two points black. We then switch to 3 points wide for the next inch, and then 4 points wide, etc. This trend continues for the entire length of the page (or for 12 inches of run length in the case of a printer using continuous roll stationery). After printing the test pattern the page is ejected (or the test pattern is rolled clear of the printer enclosure, in the case of continuous roll printers). Note that it is an acceptable solution to use the smallest marks that the language provides, rather than working at native printer resolution, where this is not achievable from within the language. Optionally, on systems where the printer resolution cannot be determined, it is permissible to prompt the user for printer resolution, and to calculate point size based on user input, enabling fractional point sizes to be used.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pinstripe/Printer step by step in the Wren programming language
Source code in the wren programming language
/* gcc -O3 -std=c11 -shared -o printer.so -fPIC -I./include printer.c */
#include <stdlib.h>
#include <string.h>
#include "dome.h"
static DOME_API_v0* core;
static WREN_API_v0* wren;
static const char* source = ""
"class Printer {\n"
"foreign static printFile(name) \n"
"} \n";
void C_printFile(WrenVM* vm) {
const char *arg = wren->getSlotString(vm, 1);
char command[strlen(arg) + 4];
strcpy(command, "lp ");
strcat(command, arg);
int res = system(command);
}
DOME_EXPORT DOME_Result PLUGIN_onInit(DOME_getAPIFunction DOME_getAPI, DOME_Context ctx) {
core = DOME_getAPI(API_DOME, DOME_API_VERSION);
wren = DOME_getAPI(API_WREN, WREN_API_VERSION);
core->registerModule(ctx, "printer", source);
core->registerClass(ctx, "printer", "Printer", NULL, NULL);
core->registerFn(ctx, "printer", "static Printer.printFile(_)", C_printFile);
return DOME_RESULT_SUCCESS;
}
DOME_EXPORT DOME_Result PLUGIN_preUpdate(DOME_Context ctx) {
return DOME_RESULT_SUCCESS;
}
DOME_EXPORT DOME_Result PLUGIN_postUpdate(DOME_Context ctx) {
return DOME_RESULT_SUCCESS;
}
DOME_EXPORT DOME_Result PLUGIN_preDraw(DOME_Context ctx) {
return DOME_RESULT_SUCCESS;
}
DOME_EXPORT DOME_Result PLUGIN_postDraw(DOME_Context ctx) {
return DOME_RESULT_SUCCESS;
}
DOME_EXPORT DOME_Result PLUGIN_onShutdown(DOME_Context ctx) {
return DOME_RESULT_SUCCESS;
}
import "graphics" for Canvas, Color, ImageData
import "dome" for Window
import "plugin" for Plugin
Plugin.load("printer")
import "printer" for Printer
class Main {
construct new() {
Window.title = "Pinstripe - printer"
_width = 842
_height = 595
Canvas.resize(_width, _height)
Window.resize(_width, _height)
var colors = [
Color.hex("FFFFFF"), // white
Color.hex("000000") // black
]
pinstripe(colors)
}
pinstripe(colors) {
var w = _width
var h = (_height/7).floor
for (b in 1..11) {
var x = 0
var ci = 0
while (x < w) {
var y = h * (b - 1)
Canvas.rectfill(x, y, b, h, colors[ci%2])
x = x + b
ci = ci + 1
}
}
}
init() {
var img = ImageData.create("pinstripe", _width, _height)
for (x in 0..._width) {
for (y in 0..._height) img.pset(x, y, Canvas.pget(x, y))
}
img.saveToFile("pinstripe.png")
Printer.printFile("pinstripe.png")
}
update() {}
draw(alpha) {}
}
var Game = Main.new()
You may also check:How to resolve the algorithm Sorting algorithms/Shell sort step by step in the Sidef programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the Dyalect programming language
You may also check:How to resolve the algorithm A+B step by step in the Pure programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the Rust programming language
You may also check:How to resolve the algorithm Flatten a list step by step in the ALGOL 68 programming language