Point in polygon - faulty algorithm - c

I have this C code of an alogrithm checking whether a given point is inside a polygon. It is supposed to be correct, I also keep seeing this code in various places. However when I use it doesn't work perfectly - about 20% of the answers are wrong.
int pnpoly(int nvert, double *vertx, double *verty, double testx, double testy)
{
int i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c = !c;
}
return c;
}
Maybe there is something wrong with my main function. Could somone give me a main function to check this algorthm?
This is my main function
int main(){
double vertx[4] = {10, 10, 0, 0};
double verty[4] = {10, 0, 10, 0};
// for those two it returns "Inside"
double testx = 6;
double testy = 4;
/* for those two it returns "Outside"
double testx = 5;
double testy = 4;
*/
int result = pnpoly(4, vertx, verty, testx, testy);
if (result) {
printf("\nInside\n");
}
else {
printf("\nOutside\n");
}
return 0;
}

Your polygon is self intersecting. It's normal that (5,4) is "Outside"
I think you thought that your polygon was a square, the algo works perfectly even with self intersecting polygons.

Related

C Keep Getting Double Free, despite trying to free in same form as allocation

Hey I'm trying to do a simple machine learning application for school but I keep getting double free for some reason I cannot even fathom.
float * evaluate(Network net,float * in)
{
int i,j;
float * out;
Neuron cur_neu;
for(i=0,j=0;i<net.n_lay;i++) j = net.lay_sizes[i]>j?net.lay_sizes[i]:j; //Calculating the maximum lay size for output storage
out = (float *) malloc(j*sizeof(float));
for(i=0;i<net.n_lay;i++) //Cycling through layers
{
for(j=0;j<net.lay_sizes[i];j++) //Cycling through Neurons
{
cur_neu=net.matrix[i][j];
out[j] = cur_neu.af(cur_neu.w,in,net.lay_sizes[i-1]); //Storing each answer in out
}
for(j=0;j<net.lay_sizes[i];j++) in[j] = out[j]; //Transfering answers to in
}
return out;
}
float loss(Network net, float **ins_orig, int t_steps)
{
float **profecies;
float st = .5f;
int d_steps = 4;
int t, i, j;
int out_size = net.lay_sizes[net.n_lay - 1];
int in_size = net.lay_sizes[0];
float out = 0.0f;
float **ins;
/*
d_steps = Divination Steps: Number of time steps forward the network has to predict.
The size of the output layer must be d_steps*#ins (deconsidering any conceptual i/os)
t_steps = Total of Steps: Total number of time steps to simulate.
*/
//Copying ins
ins = (float **)malloc(t_steps * sizeof(float *));
for (i = 0; i < t_steps; i++) //I allocate memory for and copy ins_orig to ins here
{
ins[i] = (float *)malloc(in_size * sizeof(float));
for (j = 0; j < in_size; j++)
ins[i][j] = ins_orig[i][j];
}
//
profecies = (float **)malloc(t_steps * sizeof(float *));
for (t = 0; t < t_steps; t++)
{
profecies[t] = evaluate(net, ins[t]);
/*
Profecy 0:
[[a1,b1,c1,d1]
[e1,f1,g1,h1]
[i1,j1,k1,l1]]
Profecy 1:
[[e2,f2,g2,h2]
[i2,j2,k2,l2]
[m2,n2,o2,q2]]
Verification for:
t=0:
loss+= abs(a1-ins[t][0]+b2-ins[t][1]...)
t=1:
t=0:
loss+= abs(e1-ins[t][0]+f2-ins[t][1]...)
*/
for (i = 0; i < d_steps; i++) //i is distance of prediction
{
if (i <= t) // stops negative profecy indexing
{
for (j = 0; j < in_size; j++)
{
out += (ins[t][j] - profecies[t-i][j+in_size*i]) * (ins[t][j] - profecies[t-i][j+in_size*i]) * (1 + st*i); //(1+st*i) The further the prediction, the bigger reward
}
}
}
}
//Free ins
for (i = 0; i < t_steps; i++) //I try to free it here, but to no avail
{
free(ins[i]);
}
free(ins);
return out;
}
I realize it's probably something very obvious but, I can't figure it out for the life of me and would appreciate the help.
Extra details that probably aren't necessary:
evaluate just passes the input to the network (stored in ins) and returns the output
both inputs and outputs are stored in float "matrixes"
Edit: Added evaluate
In your loss() you allocate the same number of floats for each ins:
ins[i] = (float *)malloc(in_size * sizeof(float));
In your evaluate() you calculate the longest lay_size, indicating that it may NOT be net.lay_sizes[0]:
for(i=0,j=0;i<net.n_lay;i++) j = net.lay_sizes[i]>j?net.lay_sizes[i]:j; //Calculating the maximum lay size for output storage
Then you are writing out-of-bounds here:
for(j=0;j<net.lay_sizes[i];j++) in[j] = out[j]; //Transfering answers to in
From that point, your memory is corrupted.

I am trying to add vectors in a for loop, but after 2 iterations I get an incorrect answer (C)

I am trying to implement the iterative formula Xn+1 = T*Xn + C, for a given number of iterations i. Here X and C are vectors and T is a 3x3 matrix. However, if I go beyond 2 iterations (fori=0;i<2;i++) the formula starts giving an incorrect result and I am not sure why. Any help is appreciated.
#include <stdio.h>
int main(void) {
double T[3][3] = {{0.0,-0.5,-0.75},
{0.6,0.0,0.4},
{0.25,-0.375,0.0}};
double C[3] = {2.0,2.8,3.375};
double x_n[3] = {0.0,0.0,0.0};
double x_n_plus_1[3] = {0.0,0.0,0.0};
double Tx[3] = {0.0,0.0,0.0};
int i, row, col, ctr;
for(i=0;i<3;i++) {
for(row=0;row<3;row++) {
for(col=0;col<3;col++){
Tx[row] += T[row][col] * x_n[col]; //Tx_n = T*x_n
}
}
for(ctr=0;ctr<3;ctr++) {
x_n_plus_1[ctr] = Tx[ctr] + C[ctr];//x_n+1 = Tx_n + C
}
for(ctr=0;ctr<3;ctr++) {
x_n[ctr] = x_n_plus_1[ctr];
}
}
for(ctr=0;ctr<3;ctr++) {
printf("%lf\n",x_n[ctr]);
}
return 0;
}
As pmg proposed: "You need to reset the elements of Tx to zero at the beginning (or end) of each i loop". For each iteration of i, you need to re-initialize the Tx. If you go beyond 2 iterations (fori=0;i<2;i++) the formula starts giving an incorrect result because, staring second iteration, Tx is not still equal to {0, 0, 0}
#include <stdio.h>
int main(void) {
double T[3][3] = {{0.0,-0.5,-0.75},
{0.6,0.0,0.4},
{0.25,-0.375,0.0}};
double C[3] = {2.0,2.8,3.375};
double x_n[3] = {0.0,0.0,0.0};
double x_n_plus_1[3] = {0.0,0.0,0.0};
int i, row, col, ctr;
for(i=0;i<3;i++) {
double Tx[3] = {0.0, 0.0, 0.0};
for(row=0;row<3;row++) {
for(col=0;col<3;col++){
Tx[row] += T[row][col] * x_n[col]; //Tx_n = T*x_n
}
}
for(ctr=0;ctr<3;ctr++) {
x_n_plus_1[ctr] = Tx[ctr] + C[ctr];//x_n+1 = Tx_n + C
}
for(ctr=0;ctr<3;ctr++) {
x_n[ctr] = x_n_plus_1[ctr];
}
}
for(ctr=0;ctr<3;ctr++) {
printf("%lf\n",x_n[ctr]);
}
return 0;
}

Unable to read memory. Can't find what's wrong

I've got a piece of code, it's purpose is to draw a background image on one of the game levels. For this purpose I create this structure.
typedef struct crate_t {
int x = 0;
int y = 0;
int h = 0;
int w = 0;
int type = BACKGROUND;
}crate;
Then in the main function I create a 2D array
crate **Crates = (crate**)malloc(sizeof(crate)*(SCREEN_WIDTH / GrassBlock->w));
for (int i = 0; i <= SCREEN_HEIGHT/GrassBlock->h; i++) {
Crates[i] = (crate*)malloc(sizeof(crate)*(SCREEN_HEIGHT / GrassBlock->h));
}
and I pass it to the function counter = DrawLevelBG(screen, GrassBlock, Border, Crates);. The problem is that the function causes error. "Access violation writing location." at Obstacles[i][j].x = x;
int DrawLevelBG(SDL_Surface *screen, SDL_Surface *sprite, SDL_Surface *border, crate **Obstacles) {
int x = 0;
int y = 0;
int i = 0;
int j = 0;
bool condition = 0;
while (y < SCREEN_HEIGHT + sprite->h) {
DrawSurface(screen, sprite, x + (sprite->w / 2), y + (sprite->h / 2));
if (x >= SCREEN_WIDTH - sprite->w || x == 0 || y == 0 || y >= SCREEN_HEIGHT - sprite->h) {
DrawSurface(screen, border, x + (sprite->w / 2), y + (sprite->h / 2));
Obstacles[i][j].x = x;
Obstacles[i][j].y = y;
Obstacles[i][j].h = border->h;
Obstacles[i][j].w = border->w;
Obstacles[i][j].type = WALL;
i++;
if (x >= SCREEN_WIDTH - sprite->w) {
y += sprite->h;
x = 0;
j++;
condition = 1;
}
}
if (!condition) {
x += sprite->w;
}
condition = 0;
}
return i;
}
I know that these ones are caused by pointers not pointing actually to anything but I can't understand what's wrong here. Any help would be greatly appreciated. Thanks.
EDIT
I've changed my memory allocation piece of code so it looks like that now:
crate **Crates = (crate**)malloc(sizeof(crate*)*(SCREEN_WIDTH / GrassBlock->w)*(SCREEN_HEIGHT / GrassBlock->h));
for (int i = 0; i <= SCREEN_WIDTH/GrassBlock->w; i++) {
Crates[i] = (crate*)malloc(sizeof(crate)*(SCREEN_HEIGHT / GrassBlock->h));
}
According to all your replies guys. Unfortunately this doesnt solve the problem. +Important info, the function DrawLevelBG causes ERROR on the first iteration of loop.
In the first allocation you create an array from pointers. So you need to allocate memory for pointers:
crate **Crates = (crate**)malloc(sizeof(crate*)*(SCREEN_WIDTH / GrassBlock->w));
Thanks for all the help guys. The problem was iterators, not only did I make my 2D array SCREEN_HEIGHT wide and SCREEN_WIDTH high which was the opposite of what I wanted but aswell the iteration in DrawLevelBG was wrong as pointed out. I had to swap my "i" and "j" and make some corrections, so thanks alot Some programmer dude for pointing that out. Thanks alot.

SDL - Blitting an Array of SDL_Rect

Having trouble blitting an image in SDL2. I built an array of SDL_Rect using malloc and attributed to each member the usual x,y,w (width), and h (height) variables, but the images simply won't show up. Curiously, when I try blitting outside of a for loop by directly referencing the index of the element, it seems to work. Any idea what's going on?
//Not Working
int number =5;
for(int i = 0; i < number; i++){
SDL_BlitScaled(myImage, NULL, mySurface, &(myElements[i]));
}
//Works Perfectly
SDL_BlitScaled(myImage, NULL, mySurface, &(myElements[0]));
Edit: Ended up forgetting this part. Here's how the array is being generated:
SDL_Rect *myElements;
myElements = (SDL_Rect * ) malloc (number * sizeof(SDL_Rect));
generateGroup(myElements, number, 10, 10, 180, 30);
The function I am calling there looks like this:
void generateGroup(SDL_Rect* elementArray, int numberElements, int posX, int posY, int width, int height) {
int i;
for(i = 0; i < numberElements; i++) {
elementArray[i].x = posX;
posX = posX + width + 1;
elementArray[i].y = posY;
elementArray[i].w = width;
elementArray[i].h = height;
}
}
To describe the issue more precisely, the image literally only appears when I blit outside the for loop. As for the index, in addition to 0, I have also tried 1 and 2, and both worked.

Float / Array error reading from CSV

I'm missing something fundamental here but I can't seem to find out what from all my research.
I have imported a csv file, split the string into floats, and now wish to connect all points to all other points with a line. My code is as follows:
String [] data;
void setup () {
size(300, 300);
background(255);
data = loadStrings("points.csv");
for (int i = 0; i < data.length; i++) {
String [] fields = split(data[i], ',');
float t = float(fields[0]);
float n = float(fields[1]);
float x = float(fields[2]);
float y = float(fields[3]);
ellipse(x, y, 10, 10);
line(x, y, x[i], y[i]);
}
}
The error message is "The type of expression must be an array type but it resolved to float"
I'm sure this is extremely basic but, I dont understand why x[i] or y[i] are not seen as an array type.
I would really appreciate any help with this. Many thanks in advance.
Sam
*UPDATE***
An exract from the points.csv file is as follows:
219185750 rabih_takkoush 20.88521 19.49821
219185716 MoustaphaAjram 100.870896 59.515259
219185709 jinanejghosh 56.886441 35.489087
219185557 MoustaphaAjram 34.870904 78.515243
219185555 Mohammad8Itani 12.8946 49.48179
What I am trying to accomplish is plotting the various geolocations (whereby col 3 = x, col 4 = y) and then connecting all points with all other points with a line.
The following script works plotting all locations specified in the array within the script:
float[] x = { 50, 100, 150, 200,20,20 };
float[] y = { 10, 30, 20, 250,20,90 };
void setup () {
size(300, 300);
background(255);
}
void draw() {
for (int i = 0; i < x.length; i++) {
ellipse(x[i], y[i], 10, 10);
for (int j = 0; j < x.length; j++) {
line(x[j], y[j], x[i], y[i]);
}
}
}
What I wish to do is do the same, but reading columns 3 and 4 of the csv file.
You're splitting your data into iteration-scoped floats, then you try to access them as if they're both floats as well as arrays in your line() call. Try this:
String[] data;
float[] x, y, t, n;
void setup () {
size(300, 300);
data = loadStrings("points.csv");
int len = data.length;
x = new float[len];
x = new float[len];
t = new float[len];
n = new float[len];
for (int i=0; i<len; i++) {
String line = data[i];
String[] fields = split(line, ',');
t[i] = float(fields[0]),
n[i] = float(fields[1]),
x[i] = float(fields[2]),
y[i] = float(fields[3]);
}
// don't put draw instructions in setup,
// put them in draw. if you want to run once,
// issue a noLoop() so that happens.
noLoop();
}
void draw() {
float prevx = x[0], prevy = y[0];
for (int i=0, last=x.length; i<last; i++) {
ellipse(x[i], y[i], 10, 10);
line(prevx,prevy, x[i],y[i]);
prevx=x[i];
prevy=y[i];
}
}
Now we're storing the data from CVS in linked arrays that we can access throughout the sketch, rather than throwing them away after setup().
ok so if you go with the first code you made, you only need to change a few things, here is what you can do:
float[] x;
float[] y;
string[] data;
void setup () {
size(300, 300);
background(255);
data = loadStrings("points.csv");
x = new float[data.length];
y = new float[data.length];
for (int i = 0; i < data.length; i++) {
String [] fields = split(data[i], ',');
float t = float(fields[0]);
float n = float(fields[1]);
float x = float(fields[2]);
float y = float(fields[3]);
}
}
void draw() {
for (int i = 0; i < x.length; i++) {
ellipse(x[i], y[i], 10, 10);
for (int j = 0; j < x.length; j++) {
line(x[j], y[j], x[i], y[i]);
}
}
}
As you can see nothing really new, it's a mix between your initial code and the one you made for the csv.
And actually you mainly needed to declare your x and y variables as float[] instead of just float. But also there were some changes to make in the loop.
In this code you load the data in your arrays first (exactly like you did by declaring array's values in your code, but this time you read these values from your file), and then call your draw method as before.
hope it works now

Resources