Skip to content

Commit

Permalink
added HPGL commands++
Browse files Browse the repository at this point in the history
  • Loading branch information
terjeio committed Jan 23, 2022
1 parent 08d3b8a commit 4d33b2e
Show file tree
Hide file tree
Showing 15 changed files with 1,226 additions and 554 deletions.
14 changes: 14 additions & 0 deletions my_plugin/hpgl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
add_library(hpgl INTERFACE)

target_sources(hpgl INTERFACE
${CMAKE_CURRENT_LIST_DIR}/motori.c
${CMAKE_CURRENT_LIST_DIR}/hpgl.c
${CMAKE_CURRENT_LIST_DIR}/arc.c
${CMAKE_CURRENT_LIST_DIR}/clip.c
${CMAKE_CURRENT_LIST_DIR}/charset0.c
${CMAKE_CURRENT_LIST_DIR}/font173.c
${CMAKE_CURRENT_LIST_DIR}/htext.c
${CMAKE_CURRENT_LIST_DIR}/scale.c
)

target_include_directories(hpgl INTERFACE ${CMAKE_CURRENT_LIST_DIR})
42 changes: 32 additions & 10 deletions my_plugin/hpgl/arc.c
Original file line number Diff line number Diff line change
@@ -1,37 +1,59 @@
#include <inttypes.h>
#include <math.h>

#include "shvars.h"
#include "hpgl.h"
#include "arc.h"
#include "scale.h"

static int16_t arc_step;
static float arc_stepangle, arc_xc, arc_yc, arc_a0, arc_r, arc_phi;

int16_t arc_init (void)
static bool arc_cfg (user_point_t user_loc)
{
arc_phi = numpad[2] * M_PI / 180.0f;
arc_phi = hpgl_state.numpad[2] * M_PI / 180.0f;

arc_stepangle = fabsf(numpad[3]);
arc_stepangle = fabsf(hpgl_state.numpad[3]);
if (arc_phi < -0.0f)
arc_stepangle = -arc_stepangle;
arc_stepangle *= M_PI / 180.0f;

arc_step = 0;
arc_xc = numpad[0];
arc_yc = numpad[1];
arc_a0 = atan2f(user_loc.y - arc_yc, user_loc.x - arc_xc);
arc_xc = hpgl_state.numpad[0];
arc_yc = hpgl_state.numpad[1];
arc_a0 = atan2f(hpgl_state.user_loc.y - arc_yc, hpgl_state.user_loc.x - arc_xc);
if (arc_a0 < -0.0f)
arc_a0 += 2.0f * M_PI;

arc_r = hypotf(user_loc.x - arc_xc, user_loc.y - arc_yc);
arc_r = hypotf(hpgl_state.user_loc.x - arc_xc, hpgl_state.user_loc.y - arc_yc);

//printf_P(PSTR("AA: (%f,%f) r=%f stepangle=%f a0=%f aend=%f\n"),
// arc_xc, arc_yc, arc_r, arc_stepangle, arc_a0*180/M_PI, (arc_a0+arc_phi)*180/M_PI);

return arc_phi != 0.0f && arc_r != 0.0f;
}


bool arc_init (void)
{
return arc_cfg(hpgl_state.user_loc);
}

bool circle_init (hpgl_point_t *target)
{
user_point_t d;

d.x = hpgl_state.user_loc.x + hpgl_state.numpad[0];
d.y = hpgl_state.user_loc.y;

hpgl_state.numpad[2] = 360.0f;
hpgl_state.numpad[3] = hpgl_state.numpad[1];
hpgl_state.numpad[0] = hpgl_state.user_loc.x;
hpgl_state.numpad[1] = hpgl_state.user_loc.y;

userscale(d, target, &hpgl_state.user_loc);

return arc_cfg(d);
}

bool arc_next (hpgl_point_t *target)
{
bool cont = true;
Expand All @@ -50,7 +72,7 @@ bool arc_next (hpgl_point_t *target)

//printf_P(PSTR("ARC SPAN: (%6.1f %6.1f)-(%6.1f %6.1f)\n"), user_loc.x, user_loc.y, xd, yd);

userscale(d, target, &user_loc);
userscale(d, target, &hpgl_state.user_loc);

return cont;//arc_step < arc_steps ? 1 : 0;
}
7 changes: 4 additions & 3 deletions my_plugin/hpgl/arc.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#ifndef _ARC_H
#define _ARC_H

#include <inttypes.h>
#include <stdbool.h>
#include "hpgl.h"

/// Initialize the arc based on current location and scratchpad data.
/// @see numpad
/// @see user_loc
/// @returns 0 if the arc is degenerate (0 degrees or R=0)
int16_t arc_init(void);
bool arc_init(void);

bool circle_init (hpgl_point_t *target);

/// Calculate the next chord.
/// @param x next x in absolute stepper coordinates
Expand Down
2 changes: 1 addition & 1 deletion my_plugin/hpgl/charset0.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ copies.
** commands needed for switching from one to the other.
**/

char *charset0[256] = {
const char *const charset0[256] = {
/* 0x00 ... 0x1f */

/**
Expand Down
81 changes: 81 additions & 0 deletions my_plugin/hpgl/clip.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <stdint.h>
#include <stdbool.h>

// https://educatech.in/c-program-to-implement-sutherland-hodgemann-polygon-clipping-algorithm/

enum {
TOP = 0x1,
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};

typedef uint_fast8_t outcode;

float xwmin,xwmax,ywmin,ywmax;

outcode CompOutCode(float x, float y)
{
outcode code = 0;
if(y > ywmax)
code |=TOP;
else if(y < ywmin)
code |= BOTTOM;

if(x > xwmax)
code |= RIGHT;
else if(x < xwmin)
code |= LEFT;

return code;
}

void clip (float x0, float y0, float x1, float y1)
{
outcode outcode0,outcode1,outcodeOut;
bool accept = false, done = false;
outcode0 = CompOutCode(x0, y0);
outcode1 = CompOutCode(x1, y1);

do {
if(!(outcode0 | outcode1)) {
accept = true;
done = true;
}
else if(!(done = !!(outcode0 & outcode1))) {

float x, y;

outcodeOut = outcode0 ? outcode0 : outcode1;

if(outcodeOut & TOP) {
x = x0 + (x1 - x0) * (ywmax - y0) / (y1 - y0);
y = ywmax;
} else if(outcodeOut & BOTTOM) {
x = x0 + (x1 - x0) * (ywmin - y0) / (y1 - y0);
y = ywmin;
} else if(outcodeOut & RIGHT) {
y = y0 + (y1 - y0) * (xwmax - x0) / (x1 - x0);
x = xwmax;
} else {
y = y0 + (y1 - y0) * (xwmin -x0) / (x1 - x0);
x = xwmin;
}
if(outcodeOut == outcode0) {
x0 = x;
y0 = y;
outcode0 = CompOutCode(x0,y0);
} else {
x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1);
}
}
} while(!done);
/*
if(accept)
line(x0,y0,x1,y1);
outtextxy(150,20,"POLYGON AFTER CLIPPING");
rectangle(xwmin,ywmin,xwmax,ywmax);
*/
}
2 changes: 1 addition & 1 deletion my_plugin/hpgl/font173.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ copies.
** characters in the upper half of the font
**/

char *charset173[256] = {
const char *const charset173[256] = {
/* 0x00 ... 0x1f */

/**
Expand Down
Loading

0 comments on commit 4d33b2e

Please sign in to comment.