I am trying to create a program in C to check if the lines of rectilinear polygon intersect each other at any point.
I only need simple rectilinear polygons that do not intersect at any point. It can be counter-clockwise or clockwise.
The direction values will be less than 10. NS direction must alternate with WE direction and vice versa.
The inputs being passed are in the form of direction from an input file such as; and also shown in the pictures:
S 2 E 4 S 2 E 4 N 2 W 4 N 2 W 4
I tried to store points in a two-dimensional array with every point being checked as true, but I am unable to figure out how to move in case of counter and clockwise direction as the points could be N4 E6 or S4 W6. In such a case if I add when the value is N-4 (x,y) = (0,4) and subtract when S-4 (x,y) = (0,-4) that would fail when using it as index in array.
int arr[10][10];
int xPrime = 0, yPrime = 0;
bool checkContinuity(int y, const char * dir ){
if(strcmp(dir, "S")==0){
y = -y;
cols = y;
int j;
for(j = cols; j >= 0; j--){
if(arr[xPrime][j] == 1 && j != yPrime){
return false;
}
arr[xPrime][j] = 1;
printf(" %d ", j);
}
yPrime -= y;
if(yPrime < 0)
yPrime = -yPrime;
}
else if(strcmp(dir, "W")==0){
y = -y;
cols = y;
int j;
for(j = cols; j >= 0; j--){
if(arr[j][yPrime] == 1 && j != xPrime && (j != 0 && yPrime != 0)){
return false;
}
arr[j][yPrime] = 1;
printf(" %d ", j);
}
xPrime -= y;
if(xPrime < 0)
xPrime = -xPrime;
}
else if(strcmp(dir, "N")==0){
cols = y;
int j;
for(j = 0; j <= cols; j++){
if(arr[xPrime][j] == 1)
return false;
arr[xPrime][j] = 1;
printf(" %d ", j);
}
yPrime += y;
}
else if(strcmp(dir, "E")==0){
cols = y;
int j;
for(j = 0; j <= cols; j++){
if(arr[j][yPrime] == 1 && j != xPrime)
return false;
arr[j][yPrime] = 1;
printf(" %d ", j);
}
xPrime += y;
}
else
return false;
return true;
}
It might be easier to store the actual polygon rather than all possible
points of the plane. We are then not restricted by the choice of the
point array (arr in your code). See this working example:
the polygon is stored in the int-array P = {0, 0, x1, y1, x2, y2, ...}
segments are two-point polygons. The function 'intersection' checks if
two such segments Q and P intersect;
if so, returns the intersection coordinates.
It uses the helper function 'between' to ckeck if one number is between two others.
the function 'next' calculates the next point of the polygon, assuming
the input is provided as char-string (eg: "S2E4S2E4N2W4N2W4")
In the function main we now loop over all segments, and check if they intersect with
any previous one.
Of course, the input should at some point be checked for sanity etc.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void
next( const char *s,
int *v )
{
v[2] = v[0];
v[3] = v[1];
int step = s[1] - '0';
switch(s[0]) {
case 'S': v[3] -= step; break;
case 'N': v[3] += step; break;
case 'W': v[2] -= step; break;
case 'E': v[2] += step; break;
}
}
int
between( int x,
int a,
int b )
{
return a < b ? x >= a && x <= b : x >= b && x <= a;
}
int
intersection( int *P,
int *Q,
int *R )
{
if(P[0] == P[2] && Q[1] == Q[3]){ // P vertical, Q horizontal (w.l.o.g.)
if(between(P[0], Q[0], Q[2]) && between(Q[1], P[1], P[3])){
R[0] = P[0];
R[1] = Q[1];
return 1;
} else
return 0;
}else if(Q[0] == Q[2] && P[1] == P[3])
return intersection(Q, P, R);
else return 0;
}
int
main() {
char *s = "S2E4S2E4N2W4N2W4";
int n = strlen(s) / 2, // number of steps
*P = calloc((n + 1) * 2, sizeof(int)), // polygon
R[2]; // intersection
if(!P) exit(137);
for(int k = 0; k < n; k++){
next(s + 2 * k, P + 2 * k);
for(int j = 0; j < k - 1; j++) {
if(intersection(P + k * 2, P + j * 2, R)) {
printf("Intersection at: %d, %d\n", R[0], R[1]);
exit(0);
}
}
}
printf("No intersection\n");
}
Related
In line 10 I cannot find out where my problem is at first. I place int a[100][100]={0} but the cpu speed is stuck.
Then, I try to change it into a[n][n] but no output is shown.
Last, I try to change it again as if it resembles the original ones.
However, nothing works instead of a new question.
#include<stdio.h>
int main() {
int n;
while (scanf("%d", &n)) {
n *= 2;
int x = 0, y = 0, num = 1;
int a[n][n] = {0};
a[x][y] = num++;
while (n * n >= num) //定義陣列
{
while (y + 1 < n && !a[x][y + 1]) //向右
a[x][++y] = num++;
while (x + 1 < n && !a[x + 1][y]) //向下
a[++x][y] = num++;
while (y - 1 >= 0 && !a[x][y - 1]) //向左
a[x][--y] = num++;
while (x - 1 >= 0 && !a[x - 1][y]) //向上
a[--x][y] = num++;
}
for (x = 0; x < n; x++) //print 陣列
{
for (y = 0; y < n; y++) {
if (y != n - 1) {
printf("%d ", a[x][y]);
} else {
printf("%d", a[x][y]);
}
}
printf("\n");
}
break;
}
return 0;
}
At least this problem:
Variable Length Arrays (VLA) cannot be initialized via the C standard.
Alternate, assign via memset() after defining a.
// int a[n][n]={0};
int a[n][n];
memset(a, 0, sizeof a);
As you see on the image there are no * in lower part of the circle. Why is that?
Equations in the loops: y = 10 - i , x = j-10 or k-10
Circle formula = r^2 = (x-a)^2 + (y-b)^2
int i, j, k;
for (i = 0; i < 21; i++) {
if (i == 10) {
for (j = 0; j < 21; j++) {
if (pow(r * r - (j -10 -a)*(j -10 -a), 0.5) + b == 0) {
printf("*");
}
else if (j == 10) {
printf("|");
}
else {
printf("-");
}
}
}
else {
for (k = 0; k < 21; k++) {
if (pow(r * r - (k -10 -a)*(k -10 -a), 0.5) + b == 10 - i) {
printf("*");
}
else if (k == 10) {
printf("|");
}
else {
printf(" ");
}
}
}
printf("\n");
}
enter image description here
You program should be more like this:
#include <stdio.h>
#include <math.h>
int main() {
int i, j, k;
int a = 3, b = 4, r = 5;
int x, y;
for (i = 0; i < 21; i++) {
y = -(i - 10);
if (y == 0) {
for (j = 0; j < 21; j++) {
x = j - 10;
if (r * r == pow(x - a, 2) + pow(y - b, 2)) {
printf("*");
}
else if (x == 0) {
printf("|");
}
else {
printf("-");
}
}
}
else {
for (k = 0; k < 21; k++) {
x = k - 10;
if (r * r == pow(x - a, 2) + pow(y - b, 2)) {
printf("*");
}
else if (x == 0) {
printf("|");
}
else {
printf(" ");
}
}
}
printf("\n");
}
}
When you match your equation, you should allow for (-3) ^ 2 is 9, while if you take the square root of 9, you could only match a 3, unless if you also check whether the "minus" of the square root is a 3, which is troublesome.
It might also be good that if you use x and y and do the calculations, and then at the last moment, map to the screen's method of displaying it, or a similar way, instead of using i and then -(i - 10) or 10 - i every where to mean y, so I added the x and y in your program. You might also comment that (a, b) is supposed to be the center of the circle.
There is also one point about, it seems the difference between y being 0 or not is that you print out the hyphen (for the x-axis), vs a space, so your program could be:
#include <stdio.h>
#include <math.h>
int main() {
int i, j;
int a = 3, b = 4, r = 5;
int x, y;
for (i = 0; i < 21; i++) {
y = -(i - 10);
for (j = 0; j < 21; j++) {
x = j - 10;
if (r * r == pow(x - a, 2) + pow(y - b, 2)) {
printf("*");
}
else if (x == 0) {
printf("|");
}
else {
printf(y == 0 ? "-" : " ");
}
}
printf("\n");
}
}
and in fact, you don't need i and j, but can directly iterate through x and y, as your way of displaying is only one character each time:
#include <stdio.h>
#include <math.h>
int main() {
int a = 3, b = 4, r = 5;
int x, y;
for (y = 10; y >= -10; y--) {
for (x = -10; x <= 10; x++) {
if (r * r == pow(x - a, 2) + pow(y - b, 2)) {
printf("*");
}
else if (x == 0) {
printf("|");
}
else {
printf(y == 0 ? "-" : " ");
}
}
printf("\n");
}
}
I am using eclipse 3.2020 on WIN10 and I have a problem executing my main function.
When I run the program as it is, I get no output to conole, even when I add a printf in the first line, and the exit code is -1,073,741,819. When I comment out/ delete the line solve(s); the code run as intended and gives exit code 0.
Edit: added full code (both solve and print_sol are in solver.c)
Edit 2: As mentioned in the comments, the problem was in the code (bug) and not eclipse, I just assumed that an error message will be printed if there is one.
p.s.: I still find the fact a printf in the start won't print if there is a runtime error in another part of the main function quite weird.
main.c:
#include "solver.h"
#include <stdlib.h>
int main(int argc, char** argv){
int **grid = (int**) malloc(sizeof(int*) * 4);
for (int i = 0; i < 4 ; i++){
grid[i] = (int*) malloc(sizeof(int) * 4);
}
int mat[4][4] = {{1,0,3,0}
,{2,0,0,0}
,{3,0,0,0}
,{4,2,0,0}};
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
grid[i][j] = mat[i][j];
}
}
solver *s = create_solver(4, &grid);
solve(s);
print_sol(s);
}
solver.h:
#ifndef SOLVER_H_
#define SOLVER_H_
typedef struct sudoku_solver solver;
/*creates a new solver using the length of one row of the board.
*Then, the user will follow the instructions on screen to input the board*/
solver* create_solver(int row_len, int ***input_board_ptr);
/*if solver is NULL, an error will appear.
*Otherwise, The board that was given won't be changed, and neither
*the solver nor the solution (unless saved before using get_sol)
*will be accessible after this*/
void destroy_solver(solver *solver);
/*if solver is NULL, an error will appear.
*Otherwise, it will solve the inputed board*/
void solve(solver *solver);
/*if "solve" wasn't executed before, an error will appear.
*Otherwise, it will print a solution to the inputed board*/
void print_sol(solver *solver);
/*if "solve" wasn't executed before, an error will appear.
*Otherwise, returns a solution to the inputed board as a matrix of integers*/
int** get_sol(solver *solver);
#endif /* SOLVER_H_ */
solver.c:
#include "solver.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
/*the board will be represented by an array of size NxN.
*the value of every board cell is between 0 and N when 0
*means "default value"*/
typedef struct sudoku_solver{
/*length of one row of the board*/
int N;
/*a pointer to the solution board*/
int ***sol_ptr;
}solver;
solver* create_solver(int row_len, int ***input_board_ptr){
solver *s = (solver*) malloc(sizeof(solver));
/*throw an ERROR if the malloc failed*/
/*row_len is a variable, so we have to declare everything dynamically */
/*allocating the sol matrix as an array of pointers (1 out of 2D)*/
int **sol = (int**) malloc(row_len * sizeof(int*));
for (int i = 0; i < row_len; i++){
/*allocating every row (the second D)
*while making sol equal to input_board*/
sol[i] = (int*) malloc(row_len * sizeof(int));
for (int j = 0; j < row_len; j++){
sol[i][j] = (*input_board_ptr)[i][j];
}
}
s->N = row_len;
/*if row_len != pow(sqrt(row_len),2) then throw invalid input ERROR*/
s->sol_ptr = /
return s;
}
void destroy_solver(solver *s){
for (int i = 0; i < s->N; i++){
free((*(s->sol_ptr))[i]);
}
free(*(s->sol_ptr));
free(s->sol_ptr);
free(s);
}
int* calc_next(int x, int y, int *next, solver *s);
bool isSafe(int x, int y, int val, solver *s);
bool solve_rec(int x, int y, solver *s);
void solve(solver *s){
int n = s->N;
int next[2];
int ***sp = s->sol_ptr;
//find next empty space
if ((*sp)[0][0] == 0){
next[0] = 0;
next[1] = 1;
}
else{
calc_next(0, 0, next, s);
}
int nextX = next[0];
int nextY = next[1];
for (int i = 1; i < n; i++){
if (isSafe(nextX, nextY, i, s)){
(*sp)[nextX][nextY] = i;
if(solve_rec(nextX, nextY, s)){
return;
}
//backtrack
(*sp)[nextX][nextY] = 0;
}
}
printf("no sol");
return;
}
bool solve_rec(int x, int y, solver *s){
int n = s->N;
int next[2];
int ***sp = s->sol_ptr;
if (x == n - 1 && y == n - 1){
return true;
}
//find next empty space
calc_next(x, y, next, s);
int nextX = next[0];
int nextY = next[1];
for (int i = 1; i < n; i++){
if (isSafe(nextX, nextY, i, s)){
(*sp)[nextX][nextY] = i;
if(solve_rec(nextX, nextY, s)){
return true;
}
//backtrack
(*sp)[nextX][nextY] = 0;
}
}
return false;
}
bool isSafe(int x, int y, int val, solver *s){
int n = s->N;
int ***sp = s->sol_ptr;
/*check row*/
for (int j = 0; j < n; j++){
if ((*sp)[x][j] == val){
return false;
}
}
/*check col*/
for (int i = 0; i < n; i++){
if ((*sp)[i][y] == val){
return false;
}
}
/*check block
*the index of a block in a grid is just like the index of entry in block.
*In sudoku, there are bs*bs blocks, and each has bs rows and bs columns*/
int bs = sqrt(n); // block size
int block_x_index = x / bs;
int block_y_index = y / bs;
for(int i = block_x_index * bs; i < bs * (block_x_index + 1); i++){
for(int j = block_y_index * bs; j < bs * (block_y_index + 1); j++){
if ((*sp)[i][j] == val){
return false;
}
}
}
return true;
}
/*assuming x,y is not the last place in the grid,
* finds the next empty place after it*/
int* calc_next(int x, int y, int *next, solver *s){
int n;
int ***sp = s->sol_ptr;
/*find the first empty place*/
do{
n = s->N;
if (y == n - 1){
x++;
y = 0;
}
else{
y++;
}
}while ((*sp)[x][y] != 0);
next[0] = x;
next[1] = y;
return next;
}
void print_sol(solver *s){
int n = s->N;
int bs = sqrt(n); // block size
char curr;
int rows_passed, col_passed;
for (int i = 0; i < n + bs - 1; i++){
for (int j = 0; j < n + bs - 1; j++){
//if it's a grid row
if (i == bs || ((i - bs) % (bs + 1)) == 0){
//if it's also a grid col
if (j == bs || ((j - bs) % (bs + 1) == 0)){
curr = '+';
}
else{
curr = '-';
}
}
//if it's only a grid col
else if (j == bs || ((j - bs) % (bs + 1) == 0)){
curr = '|';
}
else{
rows_passed = i / (bs + 1);
col_passed = j / (bs + 1);
curr = '0' + (*(s->sol_ptr))[i-rows_passed][j-col_passed];
}
printf("%c",curr);
}
printf("\n");
}
}
int** get_sol(solver *solver){
return *(solver->sol_ptr);
}
Thank you.
Please learn how to use your debugger. In this case, it would take you directly to the problem: you're crashing with an access violation (Windows 0xc0000005) here:
void solve(solver *s) {
int n = s->N;
int next[2];
int ***sp = s->sol_ptr;
//find next empty space
if ((*sp)[0][0] == 0) { // <-- Access violation here: "sp" incorrectly initialized!
next[0] = 0;
next[1] = 1;
}
The underlying problem is that although sudoku_solver.N was initialized to "4" ... sudoku_solver.sol_ptr[0][0] is pointing to uninitialized memory.
PS:
Yes, it's very definitely "executing". It wouldn't crash if it didn't run ;)
Apologies for the horrible indentation. For whatever reason it wont paste with it.
As in the title Im getting a segmentation error pointing to both main and ReadData funtion. However it doesnt say in which line. Ive tried multiple changes and it end sup in the same.
edit: gdb gives me:at vfscanf.c:1898 1898 vfscanf.c: No such file or directory.
As input Im using in.txt with:
-1 1 5 14
3 1
-6 -2
-4 2
4 -4
2 4
-1 3
2 2
0 -2
-4 -2
-6 6
4 4
-2 4
2 -2
-4 6
C CODE:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/* point structure to store x & y co-ordinates */
typedef struct {
int x, y;
}
point;
/* Prototype declarations*/
point * ReadData(point * center, int * radius, int * nPoints);
point * FilterData(point * data, point center, int radius, int * nPoints);
void Merge(point * data, int p, int q, int r);
void MergeSort(point * data, int p, int r);
void BinarySearch(point * data, int nPoints, point p);
void SearchPhase(point * data, int nPoints);
int main() {
point * data, * filter;
point center;
FILE * fp = fopen("out.txt", "w"); // output file out.txt
int radius, nPoints, i;
data = ReadData( & center, & radius, & nPoints); //call of ReadData()
filter = FilterData(data, center, radius, & nPoints); // call of FilterData()
MergeSort(filter, 0, nPoints - 1); // call of MersgeSort()
for (i = 0; i < nPoints; i++)
fprintf(fp, "%d\t%d\n", filter[i].x, filter[i].y); // writing to output file
fclose(fp);
printf("Filtered and sorted data written to out.txt");
SearchPhase(filter, nPoints); // call of SearchPhase()
return 0;
}
point * ReadData(point * center, int * radius, int * nPoints) {
FILE * fp = fopen("in.txt", "r"); // input file in.txt
point * ptr;
int i, n;
/* reading of center radius and no. of poins from file*/
fscanf(fp, "%d%d%d%d", & center - > x, & center - > y, radius, nPoints);
n = * nPoints;
ptr = (point * ) malloc(sizeof(point) * n); // dynamic memory allocation
for (i = 0; i < n; i++)
fscanf(fp, "%d%d", & ptr[i].x, & ptr[i].y); // reading of x and y from file
fclose(fp);
return ptr;
}
point * FilterData(point * data, point center, int radius, int * nPoints) {
point * filter;
int n = * nPoints, i, j, pos, x, y;
double dist;
int * a = (int * ) calloc(sizeof(int), n); // dynamic memory allocation
for (i = 0, j = 0; i < n; i++) {
x = data[i].x;
y = data[i].y;
dist = sqrt(pow(center.x - x, 2) + pow(center.y - y, 2)); // distance between center and point
if (dist <= (double) radius) // if distance <= radius then point is within circle
{
a[j] = i;
j++;
}
}
* nPoints = j;
filter = (point * ) malloc(sizeof(point) * j); // dynamic memory allocation
for (i = 0; i < j; i++) {
pos = a[i];
filter[i] = data[pos];
}
return filter;
}
/* Merge()*/
void Merge(point * data, int p, int q, int r) {
int n1 = q - p + 1;
int n2 = r - q;
point L[n1 + 1], R[n2 + 1];
int i, j, k;
for (i = 0; i < n1; i++)
L[i] = data[p + i];
for (j = 0; j < n2; j++)
R[j] = data[q + 1 + j];
point sentinel;
sentinel.x = 999;
sentinel.y = 999;
L[n1] = sentinel;
R[n2] = sentinel;
i = 0, j = 0;
for (k = p; k <= r; k++) {
if (L[i].x < R[j].x || (L[i].x == R[j].x && L[i].y < R[j].y)) {
data[k] = L[i];
i++;
} else {
data[k] = R[j];
j++;
}
}
}
/* MergeSort()*/
void MergeSort(point * data, int p, int r) {
int q, i;
if (p < r) {
q = (p + r) / 2;
MergeSort(data, p, q);
MergeSort(data, q + 1, r);
Merge(data, p, q, r);
}
}
/* BinarySearch()*/
void BinarySearch(point * data, int nPoints, point p) {
int l = 0, u = nPoints - 1, m, flag = 0;
while (l <= u) {
m = (l + u) / 2;
if (data[m].x == p.x && data[m].y == p.y) {
flag = 1;
break;
} else if (p.x < data[m].x || (p.x == data[m].x && p.y < data[m].y))
u = m - 1;
else
l = m + 1;
}
if (flag)
printf("\nOutput: Found at record %d", m + 1);
else
printf("\nOutput: Not Found");
}
/*SearchPhase()*/
void SearchPhase(point * data, int nPoints) {
point p;
while (1) {
printf("\nSearch input ( x y): ");
scanf("%d%d", & p.x, & p.y);
if (p.x == -999 || p.y == -999) {
printf("\nOutput: Exit\n");
break;
}
BinarySearch(data, nPoints, p);
}
}
I've decided to create a simple demo, dividing a polygon into triangle set. Here what i've got so far:
A sequential vertex list is given (P1) forming polygon edges (polygon is not convex in most cases); a triangle set is needed
Loop through all the vertices within the polygon P1 and find the one (v), which will satisfy next clauses:
remove v from polygon and save the new one to P2 previous vertex to v
connected to its next one form a
line which do not cross any of P2
edges
v is not inside P2
If these are satisfied, we can replace P1 with (P2 + triangle( prev(v), v, next(v)) ) and repeat this action until P1 contains more than 3 vertices.
So, the questions are: is this algorithm correct and how it could be implemented using C / C++ using the most obvious and simple way?
I think you're describing the ear clipping method. There's code for that method at http://cs.smith.edu/~orourke/books/ftp.html ; it's the code described in the book Computational Geometry in C.
Seems that i'm done with this algorithm implementation. Please, verify it someone. Thanks!
typedef struct Point
{
float x, y;
};
class MooPolygon
{
private:
vector<Point> points;
int isVertexEar(int n, const vector<Point> &p)
{
return (isVertexInsideNewPoly(n, p) && !isEdgeIntersect(n, p));
}
int isEdgeIntersect(int n, const vector<Point> &p)
{
Point v = p[n];
vector<Point> a;
for (size_t i = 0; i < p.size(); i++)
if (i != n)
a.push_back(p[i]);
int c = 0, cnt = a.size(), prev = (cnt + (n - 1)) % cnt, next = n % cnt;
Point v1 = a[prev], v2 = a[next];
for (size_t i = 0, j = cnt - 1; i < cnt; j = i++)
{
if (prev == i || prev == j || next == i || next == j)
continue;
Point v4 = a[j], v3 = a[i];
float denominator = ((v4.y - v3.y) * (v2.x - v1.x)) - ((v4.x - v3.x) * (v2.y - v1.y));
if (!denominator)
continue;
float ua = (((v4.x - v3.x) * (v1.y - v3.y)) - ((v4.y - v3.y) * (v1.x - v3.x))) / denominator;
float ub = (((v2.x - v1.x) * (v1.y - v3.y)) - ((v2.y - v1.y) * (v1.x - v3.x))) / denominator;
//float x = v1.x + (ua * (v2.x - v1.x)), y = v1.y + (ua * (v2.y - v1.y));
if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1)
{
c = 1;
break;
}
}
return c;
}
int isVertexInsideNewPoly(int n, const vector<Point> &p)
{
Point v = p[n];
vector<Point> a;
for (size_t i = 0; i < p.size(); i++)
if (i != n)
a.push_back(p[i]);
int c = 1;
for (size_t i = 0, j = a.size() - 1; i < a.size(); j = i++)
{
if ((((a[i].y <= v.y) && (v.y < a[j].y)) || ((a[j].y <= v.y) && (v.y < a[i].y))) && (v.x > (a[j].x - a[i].x) * (v.y - a[i].y) / (a[j].y - a[i].y) + a[i].x))
c = !c;
}
return c;
}
float dist(Point a, Point b)
{
return sqrt( ((a.x - b.x) * (a.x - b.x)) + (((a.y - b.y) * (a.y - b.y))) );
}
public:
void push(const Point &p)
{
for (size_t i = 0; i < points.size(); i++)
{
if (dist(points[i], p) < 7.f)
{
points.push_back(points[i]);
return;
}
}
points.push_back(p);
}
void pop()
{
if (points.size() > 0)
points.pop_back();
}
void clear()
{
points.clear();
}
Point v(int index)
{
return points[index];
}
size_t size()
{
return points.size();
}
void triangulate()
{
vector<Point> a;
for (size_t i = 0; i < points.size(); i++)
{
a.push_back(points[i]);
}
points.clear();
for (size_t t = a.size() - 1, i = 0, j = 1; i < a.size(); t = i++, j = (i + 1) % a.size())
{
if (a.size() == 3)
{
points.push_back(a[0]);
points.push_back(a[1]);
points.push_back(a[2]);
break;
}
if (isVertexEar(i, a))
{
points.push_back(a[t]);
points.push_back(a[i]);
points.push_back(a[j]);
a.erase(a.begin() + i, a.begin() + i + 1);
t = a.size() - 1;
i = 0;
j = 1;
}
}
}
};
The code has an error on the line below. The line is in the for loop in the push() function of your class:
points.push_back(points[i]);
You are not passing the pushed Point, but an empty element of the vector itself. I changed the line to
points.push_back(p);
and it worked.