Hi! (beginner)
In my code below, the pxx and the pyy arrays provide circular reference points around my test XY point for an early "warning zone" .... sounding off a beeper.
This code works, but being hard coded ..... makes it hard to change. Is there a better way to create this "circle of points"?
ie: lets say I want a 3ft circle of points around my test point, I just enter a "3" and the code calculates the 8 circle point coordinates into the array for me.
Thanks!
Code:#include <stdio.h> #include <stdlib.h> #include <math.h> int main(int argc, char** argv) { int bi = 8; //Set Boundry loop indexing variable (Number of Points in polygon) int ci = 9; // Set loop indexing for 8 circle points plus the test point float x[bi]; float y[bi]; float pxx[ci]; float pyy[ci]; float px=16;// Real Test X location point float py=13.77; // Real Test Y location point /* The coordinates of the test point */ pxx[0]=px;// Real Test X location point pyy[0]=py; //Real Test Y location point /*8 X,Y points circled around the real XY location for a circular WARNING ZONE)*/ pxx[1] = px; pyy[1] =(py+1); pxx[2] = px; pyy[2] =(py-1); pxx[3] =(px+1); pyy[3] = py; pxx[4] =(px-1); pyy[4] = py; pxx[5] =(px+.75); pyy[5] =(py+.75); pxx[6] =(px+.75); pyy[6] =(py-.75); pxx[7] =(px-.75); pyy[7] =(py+.75); pxx[8] =(px-.75); pyy[8] =(py-.75); float x1; float x2; float k; /* The points creating the polygon (INVISIBLE FENCE YARD BOUNDRY) */ x[0] = 7; y[0] = 22; x[1] = 10; y[1] = 10; x[2] = 7; y[2] = 2; x[3] = 15; y[3] = 7; x[4] = 21; y[4] = 6; x[5] = 30; y[5] = 14; x[6] = 22; y[6] = 18; x[7] = 20; y[7] = 26; int armedflag = 0; int cnt = 0; int status_a = 0; int status_b = 0; int status_c = 0; for ( int v = 0; v < 30; v++ ){ // Simulated interrupt from GPS (once every second) for ( int c = 0; c < ci; c++ ){ int crossings = 0; /* Iterate through each line */ for ( int i = 0; i < bi; i++ ){ /* This is done to ensure that we get the same result when the line goes from left to right and right to left */ if ( x[i] < x[ (i+1)%bi ] ){ x1 = x[i]; x2 = x[(i+1)%bi]; } else { x1 = x[(i+1)%bi]; x2 = x[i]; } /* First check if the ray is possible to cross the line */ if ( pxx[c] > x1 && pxx[c] <= x2 && ( pyy[c] < y[i] || pyy[c] <= y[(i+1)%bi] ) ) { static const float eps = 0.000001; /* Calculate the equation of the line */ float dx = x[(i+1)%bi] - x[i]; float dy = y[(i+1)%bi] - y[i]; float k; if ( fabs (dx) < eps ){ k = INFINITY; //REM: <include math.h> !!!! } else { k = dy/dx; } float m = y[i] - k * x[i]; /* Find if the ray crosses the line */ float y2 = k * pxx[c] + m; if ( pyy[c] <= y2 ){ crossings++; } } } if (( crossings % 2 == 1)&& (c == 0)) { printf("The dog is OK INSIDE the yard\n"); status_a = 1; armedflag = 1; } if (( crossings % 2 != 1)&& (c > 0)&& (armedflag == 1)) { printf("BEEP!! BEEP!! BEEP!! The dog is IN THE WARNING ZONE\n"); status_b = 1; } if (( crossings % 2 == 1)&& (c > 0) && (status_a == 0)) { armedflag = 0; } if (( crossings % 2 != 1)&& (c == 0 )&& (armedflag == 1)) { printf("ZAP!! ZAP!! ZAP!! The dog is OUTSIDE the yard\n"); status_c = 1; armedflag = 0; } } printf("Armed Flag: %d\n",armedflag); printf("Status_a: %d\n",status_a); printf("Status_b: %d\n",status_b); printf("Status_c: %d\n",status_c); } }



1Likes
LinkBack URL
About LinkBacks


