I have a bool function used in a test to see whether or not two objects are neighbours. If they are, a previous function InitFaceCheck() will write either 0 or 1 into a global array N[16] (16 cases to check for).
for (n = 0; n < count; n++ )
{
int l = 0;
for (m = 0; m < count; m++ )
{ InitFaceCheck(tet[n], tet_copy[m]); //
if( (tet[n].ID != tet_copy[m].ID) && (isNeighbour(N) == 1) ) // if T's
{
for(j = 0; j < 16; j++){
printf("N[%i] = %i ",j, N[j]);
}
printf("\n");
tet[n].next[l] = tet_copy[m].ID; // mark them as neighbours
l++; // neighbour counter
};
}
}
The InitFaceCheck() function evaluates if any the two tetrahedra share any of their faces (i.e. they share a face if the share the x,y,z coords of the three vertices that constitute that face) in total there are 16 tests, only 2 are included here:
void InitFaceCheck (TETRA a, TETRA b)
{
/*... Setup of bool variables to be used in CheckFace () ....
a bit tedious due to having .x .y .z components but it is solid for our purposes ... */
bool f11 = ( (a.vert[0].x == b.vert[0].x) && (a.vert[0].y == b.vert[0].y) && (a.vert[0].z == b.vert[0].z) &&
(a.vert[1].x == b.vert[1].x) && (a.vert[1].y == b.vert[1].y) && (a.vert[1].z == b.vert[1].z) &&
(a.vert[2].x == b.vert[2].x) && (a.vert[2].y == b.vert[2].y) && (a.vert[2].z == b.vert[2].z) );
bool f12 = ( (a.vert[0].x == b.vert[0].x) && (a.vert[0].y == b.vert[0].y) && (a.vert[0].z == b.vert[0].z) &&
(a.vert[1].x == b.vert[3].x) && (a.vert[1].y == b.vert[3].y) && (a.vert[1].z == b.vert[3].z) &&
(a.vert[2].x == b.vert[2].x) && (a.vert[2].y == b.vert[2].y) && (a.vert[2].z == b.vert[2].z) );
.......
// write output of tests to global array N, so as to make them accessible to all functions
N[0] = f11;
N[1] = f12;
N[2] = f13;
N[3] = f14;
N[4] = f21;
N[5] = f22;
N[6] = f23;
N[7] = f24;
N[8] = f31;
N[9] = f32;
N[10] = f33;
N[11] = f34;
N[12] = f41;
N[13] = f42;
N[14] = f43;
N[15] = f44;
The isNeighbour function looks like this:
bool isNeighbour (int a[16])
{
return (a[0] || a[1] || a[2] || a[3] || a[4] || a[5] || a[6] || a[7] || a[8]
|| a[9] || a[10] || a[11] || a[12] || a[13] || a[14] || a[15]);
// int i = 0;
//for (i = 0; i < 16; i++)
// {
// if ( a[i] == 1 ) return true;
//
// }
}
The output looks something like this:
T4092
T4100
N[0] = 0 N[1] = 0 N[2] = 0 N[3] = 0 N[4] = 0 N[5] = 0 N[6] = 0 N[7] = 0 N[8] = 1 N[9] = 0 N[10] = 0 N[11] = 0 N[12] = 0 N[13] = 0 N[14] = 0 N[15] = 0
T4101
T4120
N[0] = 0 N[1] = 0 N[2] = 1 N[3] = 0 N[4] = 0 N[5] = 0 N[6] = 0 N[7] = 0 N[8] = 0 N[9] = 0 N[10] = 0 N[11] = 0 N[12] = 0 N[13] = 0 N[14] = 0 N[15] = 0
T4169
N[0] = 0 N[1] = 0 N[2] = 0 N[3] = 0 N[4] = 0 N[5] = 0 N[6] = 0 N[7] = 0 N[8] = 0 N[9] = 0 N[10] = 0 N[11] = 0 N[12] = 1 N[13] = 0 N[14] = 0 N[15] = 0
N[0] = 0 N[1] = 1 N[2] = 0 N[3] = 0 N[4] = 0 N[5] = 0 N[6] = 0 N[7] = 0 N[8] = 0 N[9] = 0 N[10] = 0 N[11] = 0 N[12] = 0 N[13] = 0 N[14] = 0 N[15] = 0
My questions are the following:
why won't the commented out part of isNeighbour() work (it crashes)
? is the condition in the if loop correct ? (is it doing what I
think it is doing?)
why is N[] being rewritten as 0 when a tetrahedron has more than 1
neighbour (see T4169 with two lines of N[], in the second line,
N[12] was previously evaluated to be true (=1),
why is it when evaluating for the second time and it finds N[1]=1;
N[12] is reset to 0.
-and for the love of god is there anyway I could achieve a similar result but in a more elegant manner ? Also I am aware that I might
be violating basic rules of coding so please do not hesitate to
point them out!
Thank you
EDIT: this indeed C. I asked around and was told to include and bool should work just fine.
Related
I'm trying to transform the adjacency matrix into an incidence matrix of an undirected graph. For edges :
(1, 2), (1,5), (1,6), (2,3), (2,5), (3,4), (3,5), (4,5), (5,6)
Adj matrix is :
0 1 0 0 1 1
1 0 1 0 1 0
0 1 0 1 1 0
0 0 1 0 1 0
1 1 1 1 0 1
1 0 0 0 1 0
and I expect the result for the incidence matrix to be
0 1 0 0 1 1 0 0 0
1 0 1 0 1 0 0 0 0
0 1 0 1 1 0 0 0 0
0 0 1 0 1 0 0 0 0
1 1 1 1 0 1 0 0 0
1 0 0 0 1 0 0 0 0
but, my program returns this :
1 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 1 0 1 1 0 0 0 0
0 0 0 1 0 1 0 0 0
1 0 1 0 1 1 0 0 0
0 0 0 0 0 0 0 0 0
My source code :
graph constructor
Graph(int vertices, int edges)
{
this->vertices = vertices;
this->edges = edges;
edge = std::vector<Graph::Edge*>(edges);
for (int i = 0; i < edges; i++)
{
edge[i] = new Edge(this);
}
}
Graph* g = new Graph(numberOfVertices, numberOfEdges);
g->edge[0]->src = 1;
g->edge[0]->dest = 2;
g->edge[1]->src = 1;
g->edge[1]->dest = 5;
g->edge[2]->src = 1;
g->edge[2]->dest = 6;
g->edge[3]->src = 2;
g->edge[3]->dest = 3;
g->edge[4]->src = 2;
g->edge[4]->dest = 5;
g->edge[5]->src = 3;
g->edge[5]->dest = 4;
g->edge[6]->src = 3;
g->edge[6]->dest = 5;
g->edge[7]->src = 4;
g->edge[7]->dest = 5;
g->edge[8]->src = 5;
g->edge[8]->dest = 6;
for (i = 0; i < numberOfEdges; i++)
{
adjacency_matrix[g->edge[i]->src][g->edge[i]->dest] = 1;
adjacency_matrix[g->edge[i]->dest][g->edge[i]->src] = 1;
}
std::cout << "Adjacency matrix : " << std::endl;
for (i = 1; i <= numberOfVertices; i++)
{
for (j = 1; j <= numberOfVertices; j++)
{
std::cout<<adjacency_matrix[i][j]<<" ";
}
std::cout << std::endl;
}
// Incidence Matrix
int counter = 0;
for( int i = 1; i <= numberOfEdges; i++){
for(int j = i + 1; j < numberOfVertices; j++ ){
if(adjacency_matrix[i][j] == 1){
incidence_matrix[i][counter] = 1;
incidence_matrix[j][counter] = 1;
++counter;
}
}
}
for( int i = 1; i <= numberOfVertices; i++){
for(int j = 1; j <= numberOfEdges; j++){
std::cout<<incidence_matrix[i][j]<<" ";
}
std::cout<<std::endl;
}
The ideas in the code are correct. But the indexing in the array is wrong.
Indexing should start at 0. Note: this also applies when setting up the adjacency matrix.
The numbers you use to name the vertices/nodes where originally 1,2,3,4,5,6. I propose to call them 0,1,2,3,4,5. Your original edge (1,2) then becomes (0,1). But if we consistently rename all the vertices everywhere we end up with the same graph. The advantage of this new naming convention is that we can use these names directly as indices in the C++ data structures you are using. (Provided we use the corresponding integer value and not consider these names to be strings.)
The specification of the Graph becomes
Graph* g = new Graph(numberOfVertices, numberOfEdges);
g->edge[0]->src = 0;
g->edge[0]->dest = 1;
g->edge[1]->src = 0;
g->edge[1]->dest = 4;
g->edge[2]->src = 0;
g->edge[2]->dest = 5;
g->edge[3]->src = 1;
g->edge[3]->dest = 2;
g->edge[4]->src = 1;
g->edge[4]->dest = 4;
g->edge[5]->src = 2;
g->edge[5]->dest = 3;
g->edge[6]->src = 2;
g->edge[6]->dest = 4;
g->edge[7]->src = 3;
g->edge[7]->dest = 4;
g->edge[8]->src = 4;
g->edge[8]->dest = 5;
So printing the adjacency matrix becomes:
std::cout << "Adjacency matrix : " << std::endl;
for (i = 0; i < numberOfVertices; i++)
{
for (j = 0; j < numberOfVertices; j++)
{
std::cout<<adjacency_matrix[i][j]<<" ";
}
std::cout << std::endl;
}
and the calculation of the incidence matrix becomes:
// Incidence Matrix
int counter = 0;
for( int i = 0; i < numberOfVertices; i++){ //numberOfVertices!!
for(int j = i + 1; j < numberOfVertices; j++ ){
if(adjacency_matrix[i][j] == 1){
incidence_matrix[i][counter] = 1;
incidence_matrix[j][counter] = 1;
++counter;
}
}
}
for( int i = 0; i < numberOfVertices; i++){
for(int j = 0; j < numberOfEdges; j++){
std::cout<<incidence_matrix[i][j]<<" ";
}
std::cout<<std::endl;
}
Note that the order of the edges is determined now by the order in which you traverse the adjacency matrix.
I am trying to develop the code for an exercise that was assigned to me, but I can't find any way to do what I need to accomplish. The exercise in question consists of changing and printing the values of a 10x10 2d array filled with zeroes generating a pattern like this:
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 1 1 1 1 1 1 1 1
1 0 1 0 0 0 0 0 0 0
1 0 1 0 1 1 1 1 1 1
1 0 1 0 1 0 0 0 0 1
1 0 1 0 1 0 1 1 1 1
1 0 1 0 1 0 1 0 0 0
1 0 1 0 1 0 1 0 1 1
1 0 1 1 1 0 1 1 1 0
It is probably not very noticeable, but starting from the index a[9][0] and ending at a[8][9] of the array it starts to go through changing those zeros of the array, forming a snake-like pattern. Thinking about it for a while, I tried to solve it with this code that I made, but I think it has several errors within it even though the compiler doesn't mark any so evident:
#include <iostream>
using namespace std;
int main()
{
int px = 0;
int py = 9;
int a[10][10];
for(int i = 0; i <= 10; i++) {
for(int j = 0; j <= 10; j++) {
a[i][j] = 0;
}
}
if (py == 9 && px == 0) {
while(py >= 0) {
a[py][px] = 1;
py = py - 1;
}
}
if (py == 0 && px == 0) {
while(px <= 9) {
a[py][px] = 1;
px = px + 1;
}
}
if (py == 0 && px == 9) {
while(py <= 2) {
a[py][px] = 1;
py = py + 1;
}
}
if (py == 2 && px == 9) {
while(px >= 2) {
a[py][px] = 1;
px = px - 1;
}
}
if (py == 2 && px == 2) {
while(py <= 9) {
a[py][px] = 1;
py = py + 1;
}
}
if (py == 9 && px == 2) {
while(px <= 4) {
a[px][py] = 1;
px = px + 1;
}
}
if (py == 9 && px == 4) {
while(py >= 4) {
a[px][py] = 1;
py = py - 1;
}
}
if (py == 4 && px == 4) {
while(px <= 9) {
a[py][px] = 1;
px = px + 1;
}
}
if (py == 4 && px == 9) {
while(py <= 6) {
a[py][px] = 1;
py = py + 1;
}
}
if (py == 6 && px == 9) {
while(px >= 6) {
a[py][px] = 1;
px = px - 1;
}
}
if (py == 6 && px == 6) {
while(py <= 9) {
a[py][px] = 1;
py = py + 1;
}
}
if (py == 9 && px == 6) {
while(px <= 8) {
a[py][px] = 1;
px = px + 1;
}
}
if (py == 9 && px == 8) {
while(py >= 8) {
a[py][px] = 1;
py = py - 1;
}
}
if (py == 8 && px == 8) {
while(px <= 9) {
a[py][px] = 1;
px = px + 1;
}
}
for(int k=0; k<=9; k++) {
for(int l=0; l<=9; l++) {
cout << a[k][l] << " ";
}
cout << endl;
}
return 0;
}
I hope you can help me with this, still I continue learning a little the basic thing of C++, probably it has been that it has escaped me some concept that I don't know or something like that. Thanks in advance.
Kindly refer to this
In first conv function I made a general pattern without snake
Then I focused on positions from where the snake connects other rows or column
#include <bits/stdc++.h>
using namespace std;
void conv1(int a[10][10])
{
int temp=0;
for(int i=0; i<10; i++)
{
if(i%2==0)
{
for(int j=temp; j<10; j++)
{
a[i][j]=1;
a[j][i]=1;
}
}
temp=temp+1;
}
}
/*
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0
1 0 1 1 1 1 1 1 1 1
1 0 1 0 0 0 0 0 0 0
1 0 1 0 1 1 1 1 1 1
1 0 1 0 1 0 0 0 0 0
1 0 1 0 1 0 1 1 1 1
1 0 1 0 1 0 1 0 0 0
1 0 1 0 1 0 1 0 1 1
1 0 1 0 1 0 1 0 1 0
*/
void conv2(int a[10][10])
{
int temp=0;
for(int i=0; i<10; i++)
{
if(i%2!=0&&temp==0)
{
a[i][9]=1;
temp=1;
}
else if(i%2!=0)
{
temp=0;
}
}
for(int i=0; i<10; i++)
{
if(i%2!=0&&temp==0)
{
a[9][i]=1;
temp=1;
}
else if(i%2!=0)
{
temp=0;
}
}
a[9][9]=0;
}
int main(){
int a[10][10];
for(int i=0; i<10;i++)
{
for(int j=0; j<10; j++)
{
a[i][j]=0;
}
}
conv1(a);
conv2(a);
for(int i=0; i<10;i++)
{
for(int j=0; j<10; j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
I am implementing an algorithm by Moller on calculating triangle triangle intersection and I cannot quite wrap my head around something:
Given two planes of the intersecting triangles, we have their corresponding equations in the form of N*P + d = 0 where N is the cross product two vectors in the plane and P is any point on the plane. d can also be calculated.
Considering the triangles are not co-planar, their intersection will be a line L = P + tD where D is the N(first plane) * N(second plane) - this is the direction of the line, P is any point on the line and t is the parameter. The algorithm stops at calculating segments of intersection of triangles with their opposing triangles' planes in terms of t. Now if these segments overlap, the triangles intersect. But again, these segments are expressed only in terms of t. I, however, want to go further and actually get the coordinates of the intersection.
So here is the question - given the two planes, three points which lie on the planes (the points of the triangles) and the line L = P + tD where we know the D and the ts, how can I calculate the points of the segments?
Any help would be greatly appreciated,
Thank you
I am adding the code of what i've got so far:
{
//Implimentation of Tomas Moller's intesection finding algorithm with additions to determine the coorninates of the intersection
//Vertex vectors
Vector3f Va0(a.Point1.X, a.Point1.Y, a.Point1.Z);
Vector3f Va1(a.Point2.X, a.Point2.Y, a.Point2.Z);
Vector3f Va2(a.Point3.X, a.Point3.Y, a.Point3.Z);
Vector3f Vb0(b.Point1.X, b.Point1.Y, b.Point1.Z);
Vector3f Vb1(b.Point2.X, b.Point2.Y, b.Point2.Z);
Vector3f Vb2(b.Point3.X, b.Point3.Y, b.Point3.Z);
//First, find the plane equation of triangle b = Pb
//N dot X + d = 0 where X is any point on the plane
auto N2 = (Vb1 - Vb0).cross((Vb2 - Vb0));
N2.normalize();
float d2 = (N2 * (-1)).dot(Vb0);
//We'll then find the signed distance from each point of the triangle a to plane Pb
float da_0 = Round(N2.dot(Va0) + d2);
float da_1 = Round(N2.dot(Va1) + d2);
float da_2 = Round(N2.dot(Va2) + d2);
//reject intersetion if none of the points lie on the plane and all have the same sign
//meaning that the triangle a lies on one side of the triangle b
if (da_0 != 0 && da_1 != 0 && da_2 != 0 && da_0 > 0 && da_1 > 0 && da_2 > 0)
return nullptr;
if (da_0 != 0 && da_1 != 0 && da_2 != 0 && da_0 < 0 && da_1 < 0 && da_2 < 0)
return nullptr;
//do the same thing for the other triangle
auto N1 = (Va1 - Va0).cross((Va2 - Va0));
N1.normalize();
float d1 = (N1 * (-1)).dot(Va0);
float db_0 = Round(N1.dot(Vb0) + d1);
float db_1 = Round(N1.dot(Vb1) + d1);
float db_2 = Round(N1.dot(Vb2) + d1);
if (db_0 != 0 && db_1 != 0 && db_2 != 0 && db_0 > 0 && db_1 > 0 && db_2 > 0)
return nullptr;
if (db_0 != 0 && db_1 != 0 && db_2 != 0 && db_0 < 0 && db_1 < 0 && db_2 < 0)
return nullptr;
#pragma region Coplanar Triangles
if (db_0 == 0 && db_1 == 0 && db_2 == 0)
{
//if the triangles are coplanar we'll find the intersection in 2d space
return nullptr;
}
#pragma endregion
#pragma region Triangles are in 3D space
else
{
//Triangles are not coplanar and the intersection line is some line L = O + tD
//where D = N1 cross N2 - direction of the line and O is some point on it
//We will find the the intersection segment of the first triangle with the plane os the second triangle and visa versa
//If the two segments overlap, the triangles intesect
auto D = N1.cross(N2);
D.normalize();
#pragma region Point Contact Between the Triangles
//first reject intersection if one triangle only touches the other with one point
if ((da_0 == 0 && da_1 > 0 && da_2 > 0) || (da_0 == 0 && da_1 < 0 && da_2 < 0))
return nullptr;
if ((da_0 > 0 && da_1 == 0 && da_2 > 0) || (da_0 < 0 && da_1 == 0 && da_2 < 0))
return nullptr;
if ((da_0 > 0 && da_1 > 0 && da_2 == 0) || (0 > da_0 && da_1 < 0 && da_2 == 0))
return nullptr;
if ((db_0 == 0 && db_1 > 0 && db_2 > 0) || (db_0 == 0 && db_1 < 0 && db_2 < 0))
return nullptr;
if ((db_0 > 0 && db_1 == 0 && db_2 > 0) || (db_0 < 0 && db_1 == 0 && db_2 < 0))
return nullptr;
if ((db_0 > 0 && db_1 > 0 && db_2 == 0) || (0 > db_0 && db_1 < 0 && db_2 == 0))
return nullptr;
#pragma endregion
//If the above passes, proceed with 3d triangle intersection. Start with triangle a and plane Pb,
//there will be one point of a on one side of the Pb and two points of a on the other side of Pb. Let's find them:
#pragma region Triangle A
tuple<Vector3f, Vector3f> edgeA1;
tuple<Vector3f, Vector3f> edgeA2;
float dV0 = 1;
float dV1 = 1;
float dV2 = 1;
if ((da_0 > 0 && da_1 <= 0 && da_2 <= 0) || (da_0 < 0 && da_1 >= 0 && da_2 >= 0))
{
edgeA1 = make_tuple(Va1, Va0);
edgeA2 = make_tuple(Va0, Va2);
dV0 = da_1;
dV1 = da_0;
dV2 = da_2;
}
else if ((da_0 <= 0 && da_1 > 0 && da_2 <= 0) || (da_0 >= 0 && da_1 < 0 && da_2 >= 0))
{
edgeA1 = make_tuple(Va0, Va1);
edgeA2 = make_tuple(Va1, Va2);
dV0 = da_0;
dV1 = da_1;
dV2 = da_2;
}
else if ((da_0 <= 0 && da_1 <= 0 && da_2 > 0) || (da_0 >= 0 && da_1 >= 0 && da_2 < 0))
{
edgeA1 = make_tuple(Va0, Va2);
edgeA2 = make_tuple(Va2, Va1);
dV0 = da_0;
dV1 = da_2;
dV2 = da_1;
}
//pi = D * Vi where i = 0, 1, 2
auto pA_0 = D.dot(get<0>(edgeA1));
auto pA_1 = D.dot(get<0>(edgeA2));
auto pA_2 = D.dot(get<1>(edgeA2));
//The tA_1 and tA_2 define the interval of the intersection of triangle a with the line L
float tA_1 = pA_0 + (pA_1 - pA_0) * (dV0 / (dV0 - dV1));
float tA_2 = pA_1 + (pA_2 - pA_1) * (dV1 / (dV1 - dV2));
#pragma endregion
//Now repeat for triangle b
#pragma region Triangle B
tuple<Vector3f, Vector3f> edgeB1;
tuple<Vector3f, Vector3f> edgeB2;
if ((db_0 > 0 && db_1 <= 0 && db_2 <= 0) || (db_0 < 0 && db_1 >= 0 && db_2 >= 0))
{
edgeB1 = make_tuple(Vb1, Vb0);
edgeB2 = make_tuple(Vb0, Vb2);
dV0 = db_1;
dV1 = db_0;
dV2 = db_2;
}
else if ((db_0 <= 0 && db_1 > 0 && db_2 <= 0) || (db_0 >= 0 && db_1 < 0 && db_2 >= 0))
{
edgeB1 = make_tuple(Vb0, Vb1);
edgeB2 = make_tuple(Vb1, Vb2);
dV0 = db_0;
dV1 = db_1;
dV2 = db_2;
}
else if ((db_0 <= 0 && db_1 <= 0 && db_2 > 0) || (db_0 >= 0 && db_1 >= 0 && db_2 < 0))
{
edgeB1 = make_tuple(Vb0, Vb2);
edgeB2 = make_tuple(Vb2, Vb1);
dV0 = db_0;
dV1 = db_2;
dV2 = db_1;
}
//pi = D * Vi where i = 0, 1, 2
auto pB_0 = D.dot(get<0>(edgeB1));
auto pB_1 = D.dot(get<0>(edgeB2));
auto pB_2 = D.dot(get<1>(edgeB2));
//The tA_1 and tA_2 define the interval of the intersection of triangle a with the line L
float tB_1 = pB_0 + (pB_1 - pB_0) * (dV0 / (dV0 - dV1));
float tB_2 = pB_1 + (pB_2 - pB_1) * (dV1 / (dV1 - dV2));
#pragma endregion
#pragma region Determine Overlap
bool intersect = false;
float intPoint1;
float intPoint2;
//Order the segments
if (tA_1 > tA_2)
{
float temp = tA_1;
tA_1 = tA_2;
tA_2 = temp;
}
if (tB_1 > tB_2)
{
float temp = tB_1;
tB_1 = tB_2;
tB_2 = temp;
}
//Check for overlap
if (tB_1 >= tA_1 && tB_2 <= tA_2)
{
intPoint1 = tB_1;
intPoint2 = tB_2;
intersect = true;
}
else if (tA_1 >= tB_1 && tA_2 <= tB_2)
{
intPoint1 = tA_1;
intPoint2 = tA_2;
intersect = true;
}
else if (tB_1 >= tA_1 && tB_1 < tA_2 && tB_2 > tA_2)
{
intPoint1 = tB_1;
intPoint2 = tA_2;
intersect = true;
}
else if (tA_1 >= tB_1 && tA_1 < tB_2 && tA_2 > tB_2)
{
intPoint1 = tA_1;
intPoint2 = tB_2;
intersect = true;
}
#pragma endregion
if (!intersect)
return nullptr;
//If an overlap found, perform the last step of the algorithm and find the actual points of the intersection segment
else
{
}
}
#pragma endregion
}
I have a problem, I need to make the line to be closed, for example:
I put into the console this:
4
0 0
1 1
1 0
0 1
and it must return LIKE THIS:
0 0
0 1
1 1
1 0
NOT like this:
0 0
0 1
1 0
1 1
in other words:
I input the number of points, then I input coord_x and coord_y, then I need to sort them and also to make it closed
here is the code
struct Point {
int coordX;
int coordY;
};
for (int i = 1; i < sizeOfMasPoint; ++i) {
Point temp = masPoint[i];
bool is_Sorted = 0;
int j = i;
for (; j > 0; --j) {
if (temp.coordX > masPoint[j - 1].coordX) {
is_Sorted = 1;
} else {
if (temp.coordX < masPoint[j - 1].coordX) {
masPoint[j] = masPoint[j - 1];
} else {
if (temp.coordY >= masPoint[j - 1].coordY) {
is_Sorted = 1;
} else {
masPoint[j] = masPoint[j - 1];
}
}
}
if (is_Sorted == 1)
break;
}
masPoint[j] = temp;
}
it always returns this:
0 0
0 1
1 0
1 1
I need to make it closed...I don't know how
Thank you in advance!!!
I'm very new to C++ and was wondering if if there is a better way of doing this. It's going to run on an Arduino so I can't use ArrayLists or anything.
byte GetFreeCell(short x, short y)
{
byte possibleMoves[4] = {0,0,0,0};
if (y - 2 >= 0 && _grid[y - 2][x] == 0)
possibleMoves[0] = 1;
if (x + 2 < WIDTH && _grid[y][x + 2] == 0)
possibleMoves[1] = 2;
if (y + 2 < HEIGHT && _grid[y + 2][x] == 0)
possibleMoves[2] = 3;
if (x - 2 >= 0 && _grid[y][x - 2] == 0)
possibleMoves[3] = 4;
if (possibleMoves[0] == 0 && possibleMoves[1] == 0 && possibleMoves[2] == 0 && possibleMoves[3] == 0) {
return 0;
}
byte move = 0;
while(move == 0){
move = possibleMoves[random(4)];
}
return move;
}
Thanks,
Joe
byte GetFreeCell(short x, short y)
{
byte possibleMoves[4];
byte index = 0;
if (y - 2 >= 0 && _grid[y - 2][x] == 0)
possibleMoves[index++] = 1;
if (x + 2 < WIDTH && _grid[y][x + 2] == 0)
possibleMoves[index++] = 2;
if (y + 2 < HEIGHT && _grid[y + 2][x] == 0)
possibleMoves[index++] = 3;
if (x - 2 >= 0 && _grid[y][x - 2] == 0)
possibleMoves[index++] = 4;
return index ? possibleMoves[random(index)] : 0;
}
You can do yourself a favor and use this:
https://github.com/maniacbug/StandardCplusplus/#readme
Then you can sanitize your code by using standard containers.
Also, there's no ArrayList in C++. That's Java. With the above library, you can use std::vector instead.