sorry, that part with the predefined points ( corners ) i did not know / understand,
so i skipped that in my version.
but if we can verify my integration ( for position meter )
and get it corrected?calibrated
you not need much calculation,
just from the list you can show
meter to corner = meter(corner) - meter(actual position)
? if that helps ?
but the incremental calculations ( meter time / meter-total time-total )
easy can accumulate errors.
but you know, when you see how far i am OFF with the total [m] and [s].
UPDATE___
i activated your
closest corner code
and think need a other way to do it,
pls test:
// https://discourse.processing.org/t/how-to-slow-down-drawing-points/8234
// changed orientation and factor to mph ( track: 6km, in 200sec ? ) Kohler Grand Prix / Wisconsin
// v03 put in the corner again but not happy with the result
// corners near but not next on track are detected.
Table table;
int crows;
long startT, stopT=100;
int i=0; // timed global for current row
boolean debug = false; //false;
float speedf = 2.237; //3.6; //1 ; // if the speed is in m/s ? so time is in sec. if km/h 1/1000/3600 if m/h /0.621371
float offx=43.799, offy=-87.989, zoom=10000;
//copy in the corner thing again
int num_corners = 15; // A varible to set the number of corners
int closest_corner = 0;
int last_corner_passed = 0;
float closest_distance = 10000;
float a;
float c;
float[] corner_lat = new float[num_corners]; //Create an array for the coordinates for each corner's latitude
float[] corner_lon = new float[num_corners];
float[] dist = new float[num_corners];
float R = 6371e3; // meter GPS to m distance
void corner_setup() {
corner_lat[1] = 43.792018; //Assign latitude and lognitude for each corner // kll delete radians
corner_lon[1] = -87.989872;
corner_lat[3] = 43.791582;
corner_lon[3] = -87.995253;
corner_lat[5] = 43.801770;
corner_lon[5] = -87.992557;
corner_lat[6] = 43.801646;
corner_lon[6] = -87.996056;
corner_lat[7] = 43.799580;
corner_lon[7] = -87.996246;
corner_lat[8] = 43.797146;
corner_lon[8] = -87.999966;
corner_lat[11] = 43.797010;
corner_lon[11] = -88.002596;
corner_lat[12] = 43.804927;
corner_lon[12] = -87.997457;
corner_lat[14] = 43.803919;
corner_lon[14] = -87.990119;
}
void table_setup() {
size(720, 360);
table = loadTable("test.csv", "header"); // changed back from tsv to CSV ( by excel )
crows = table.getRowCount();
table.addColumn("way", Table.FLOAT);
table.addColumn("time", Table.FLOAT);
println( crows + " total rows in table");
for (int k = 0; k < crows; k++) {
TableRow row = table.getRow(k);
if ( k < crows-1) {
row.setFloat("way", way(k));
row.setFloat("time", speedf*row.getFloat("way")/row.getFloat("speed"));
}
if (debug) println("# "+k+" lat "+row.getFloat("lat") +
" lon "+row.getFloat("lon") +
" speed "+row.getFloat("speed") +
" way "+row.getFloat("way") +
" time "+row.getFloat("time") );
}
table.addColumn("waytotal", Table.FLOAT);
table.addColumn("timetotal", Table.FLOAT);
// get the avg position:
offx=0;
offy=0;
float wayt =0, timet=0;
for (int k = 0; k < crows; k++) {
TableRow row = table.getRow(k); // This is where we compute the average
offx += row.getFloat("lon");
offy += row.getFloat("lat");
wayt += row.getFloat("way");
row.setFloat("waytotal", wayt);
timet += row.getFloat("time");
row.setFloat("timetotal", timet);
}
offx /= (float)crows;
offy /= (float)crows;
println(" averagex lon "+offx+" averagey lat "+offy);
}
float way(int i) { //https://www.movable-type.co.uk/scripts/latlong.html
float delta = 0;
float lat1 = radians(table.getFloat(i, "lat"));
float lat2 = radians(table.getFloat(i+1, "lat"));
float lon1 = radians(table.getFloat(i, "lon"));
float lon2 = radians(table.getFloat(i+1, "lon"));
float x = (lon2-lon1)*cos((lat1+lat2)/2);
float y = lat2 - lat1;
delta = sqrt(sq(x) + sq(y))*R;
return delta;
}
void table_draw() {
for (int k =0; k < crows; k++ ) {
TableRow row = table.getRow(k);
float lat = row.getFloat("lat");
float lon = row.getFloat("lon");
float posx = map((lon-offx)*zoom, -180, 180, 0, width);
float posy = map((lat-offy)*zoom, 90, -90, 0, height);
if (debug) println("posx "+posx+" posy "+posy);
if ( k < i ) {
stroke(200, 0, 200);
strokeWeight(6);
} else {
stroke(0, 0, 200);
strokeWeight(3);
}
point(posx, posy);
}
}
void corner_draw() {
for ( int ic =0; ic < num_corners; ic++) {
if ( corner_lat[ic] > 0 || corner_lon[ic] > 0 ) { // mask the not used corners
noFill();
if ( ic == closest_corner ) stroke(200, 0, 0);
else stroke(200, 200, 200);
strokeWeight(1);
float posx = map((corner_lon[ic]-offx)*zoom, -180, 180, 0, width);
float posy = map((corner_lat[ic]-offy)*zoom, 90, -90, 0, height);
circle(posx, posy, 10);
text(ic, posx+10, posy+5);
if (debug) println("corner ic "+ic+" x "+posx+" y "+posy);
}
}
}
void setup() {
size(720, 360);
table_setup();
corner_setup();
}
void draw() {
background(0, 200, 0);
fill(0);
text(" pos:"+nf(table.getFloat(i, "waytotal"), 4, 1)+
" [m]; time:"+nf(table.getFloat(i, "timetotal"), 3, 1)+
" [s]; speed "+nf(table.getFloat(i, "speed"), 1, 1)+" [mph]"+
" closest_corner "+closest_corner, 20, height-20);
table_draw();
corner_distance();
corner_draw();
mytimer();
}
void mytimer() {
if ( millis() >= startT + stopT ) {
startT += stopT; // what you need to be done now
i++;
if ( i >= crows ) i = 0;
stopT = (long)table.getFloat(i, "time");
}
}
void corner_distance() {
float lat2 = radians(table.getFloat(i, "lat")); // current pos given by i
float lon2 = radians(table.getFloat(i, "lon"));
closest_corner = 0;
closest_distance = 10000;
for (int j = 0; j < num_corners; j++) {
float p1= radians(corner_lat[j]);
float p2 = lat2;
float delta_p = (lat2-radians(corner_lat[j]));
float delta_lam = (lon2-radians(corner_lon[j]));
a = sin(delta_p/2)*sin(delta_p/2)+cos(p1) * cos(p2)*sin(delta_lam/2)*sin(delta_lam/2);
c = 2 * atan2(sqrt(a), sqrt(1-a));
dist[j] = R*c;
if (dist[j] < closest_distance) {
closest_corner = j;
closest_distance = dist[j];
}
if (dist[j] < 20) {
last_corner_passed = j;
}
if (dist[j] > 10000) {
dist[j] = 0;
}
}
}
my idea is to delete your corner configuration and
make a new column in the excel file
and give there the corner number at the wanted record.
( for this i show the record on screen for easy edit file )
// https://discourse.processing.org/t/how-to-slow-down-drawing-points/8234
// changed orientation and factor to mph ( track: 6km, in 200sec ? ) Kohler Grand Prix / Wisconsin
// v03 put in the corner again but not happy with the result
// corners near but not next on track are detected.
// v04 make new column in excel file with the corner numbers at the wanted record.
Table table;
int crows;
long testT=1, startT, stopT=100;
int i=0; // timed global for current row
boolean debug = false; //false;
float speedf = 2.237; //3.6; //1 ; // if the speed is in m/s ? so time is in sec. if km/h 1/1000/3600 if m/h /0.621371
float offx=43.799, offy=-87.989, zoom=10000;
int closest_corner_number = 0, closest_corner = 0;
float closest_distance = 10000;
float R = 6371e3; // meter GPS to m distance
void table_setup() {
size(720, 360);
table = loadTable("test.csv", "header"); // changed back from tsv to CSV ( by excel )
crows = table.getRowCount();
table.addColumn("way", Table.FLOAT);
table.addColumn("time", Table.FLOAT);
println( crows + " total rows in table");
for (int k = 0; k < crows; k++) {
TableRow row = table.getRow(k);
if ( k < crows-1) {
row.setFloat("way", way(k));
row.setFloat("time", speedf*row.getFloat("way")/row.getFloat("speed"));
}
if (debug) println("# "+k+" lat "+row.getFloat("lat") +
" lon "+row.getFloat("lon") +
" speed "+row.getFloat("speed") +
" way "+row.getFloat("way") +
" time "+row.getFloat("time") );
}
table.addColumn("waytotal", Table.FLOAT);
table.addColumn("timetotal", Table.FLOAT);
// get the avg position:
offx=0;
offy=0;
float wayt =0, timet=0;
for (int k = 0; k < crows; k++) {
TableRow row = table.getRow(k); // This is where we compute the average
offx += row.getFloat("lon");
offy += row.getFloat("lat");
wayt += row.getFloat("way");
row.setFloat("waytotal", wayt);
timet += row.getFloat("time");
row.setFloat("timetotal", timet);
}
offx /= (float)crows;
offy /= (float)crows;
println(" averagex lon "+offx+" averagey lat "+offy);
}
float way(int i) { //https://www.movable-type.co.uk/scripts/latlong.html
float delta = 0;
float lat1 = radians(table.getFloat(i, "lat"));
float lat2 = radians(table.getFloat(i+1, "lat"));
float lon1 = radians(table.getFloat(i, "lon"));
float lon2 = radians(table.getFloat(i+1, "lon"));
float x = (lon2-lon1)*cos((lat1+lat2)/2);
float y = lat2 - lat1;
delta = sqrt(sq(x) + sq(y))*R;
return delta;
}
void table_draw() {
for (int k =0; k < crows; k++ ) {
TableRow row = table.getRow(k);
float lat = row.getFloat("lat");
float lon = row.getFloat("lon");
int corner = row.getInt("corner");
float posx = map((lon-offx)*zoom, -180, 180, 0, width);
float posy = map((lat-offy)*zoom, 90, -90, 0, height);
if (debug) println("posx "+posx+" posy "+posy);
if ( k < i ) {
stroke(200, 0, 200);
strokeWeight(6);
} else {
stroke(0, 0, 200);
strokeWeight(3);
}
point(posx, posy);
if ( corner > 0 ) { // draw corners
stroke(200, 200, 200);
strokeWeight(1);
noFill();
circle(posx, posy, 10);
fill(200, 200, 200);
textSize(15);
text(corner, posx+10, posy+5);
}
}
}
void setup() {
size(720, 360);
table_setup();
noFill();
}
void draw() {
background(0, 200, 0);
text_draw();
table_draw();
corner_distance();
mytimer();
}
void mytimer() {
if ( millis() >= startT + stopT ) {
startT += stopT; // what you need to be done now
i++;
if ( i >= crows ) i = 0;
stopT = (long)(table.getFloat(i, "time")*testT); // slow down for test
}
}
void corner_distance() {
for (int j = i+1; j < crows; j++ ) // walk up array in front to next corner
if ( table.getInt(j, "corner") > 0 ) {
closest_corner = j; // remember its position j
j = crows; // break for loop
}
closest_distance = table.getFloat(closest_corner, "waytotal") - table.getFloat(i, "waytotal");
closest_corner_number = table.getInt(closest_corner, "corner");
}
void text_draw() {
fill(0);
textSize(15);
text(" rec "+i+
" pos:"+nf(table.getFloat(i, "waytotal"), 4, 1)+
" [m];\n time:"+nf(table.getFloat(i, "timetotal"), 3, 1)+
" [s]; speed "+nf(table.getFloat(i, "speed"), 1, 1)+" [mph]\n"+
" nextcorner "+closest_corner_number +
"; dist " + (int)closest_distance+" [m] "
, 20, height-55);
textSize(35);
fill(200,200,200);
noStroke();
rect(width-90,10,50,50);
fill(200,0,0);
int warning = (int)(closest_distance/50.0);
if ( warning < 10 ) text(warning,width-80,50);
}
void keyPressed() {
if ( key == 's' ) {
if ( testT == 1 ) testT = 1000;
else testT = 1;
println("testT "+testT);
}
}
verify google maps