Comparing GLKMatrix4 - glkit

How would you normally compare two GLKMatrix4, or at least check if on is the identity matrix?
A cursory search of GLKMatrix4.h shows no util function, and I was feeling silly checking every field manually like this:
static BOOL GLKMatrix4EqualToMatrix4(GLKMatrix4 a, GLKMatrix4 b)
{
return
a.m00 == b.m00 &&
a.m01 == b.m01 &&
a.m02 == b.m02 &&
a.m03 == b.m03 &&
a.m10 == b.m10 &&
a.m11 == b.m11 &&
a.m12 == b.m12 &&
a.m13 == b.m13 &&
a.m20 == b.m20 &&
a.m21 == b.m21 &&
a.m22 == b.m22 &&
a.m23 == b.m23 &&
a.m30 == b.m30 &&
a.m31 == b.m31 &&
a.m32 == b.m32 &&
a.m33 == b.m33;
}

You can transform it into string and then compare using this function NSStringFromGLKMatrix4

You can do:
static BOOL GLKMatrix4EqualToMatrix4(GLKMatrix4 a, GLKMatrix4 b) {
return memcmp(a.m, b.m, sizeof(a.m)) == 0;
}
Because memcmp is usually highly optimized for specific architectures, it should be the fastest, and cleanest approach.
See Why is memcmp so much faster than a for loop check? for discussion on memcmp.

Related

Laravel Check All Multiple Requests Boolean Variable's value to True

I made an if condition for checking request boolean in laravel controller method update, it worked but the line seems too long and i want make the code in short line. And this is my code that i wrote:
if( $request->jamlak == 1
&& $request->kontrak == 1
&& $request->jamuk == 1
&& $request->sprin_pc == 1
&& $request->pc == 1
&& $request->izin_bekal == 1
&& $request->sprin_komisi == 1
&& $request->bek == 1
&& $request->komisi == 1
&& $request->bagudang== 1
&& $request->pem_gudang == 1
&& $request->bast == 1
&& $request->lpp == 1
&& $request->pemerataan == 1){
$validatedData['is_complete'] = 1;
}
else {
$validatedData['is_complete'] = 0;
}
Task::where('id', $task->id)
->update($validatedData);
return redirect('/admin/tasks')->with('success', 'New post has been updated!');
So about this code i want to make $validatedData['is_complete'] turns to be 1(true) after it checks all of requests 1 True, Is there any chance to make it way more efficient line?

How to make sure that the randomized coordinates will not fall into specific coordinates in a 2D array? The language is C

I am creating a game in C, and I want to randomly place blocks and foods inside a 2d array. I have randomized the x and y coordinates of the blocks and the food into 1d arrays, i.e. foodCol[] and foodRow[]; blockCol[] and blockRow[]. However, there comes a possibility that the blocks and foods would fall into the same coordinates. I also want them not to to be placed on the coordinate (1, 1) and beside each other, except diagonally. I came up with this code, but it does not work and maybe there are other intuitive approach.
Here is the code:
for (foodCount = 0; foodCount < randDim; foodCount++){
for (foodCount2 = 0; foodCount2 < randDim; foodCount2++){
while ((foodCol[foodCount] == blockCol[foodCount2] && foodRow[foodCount] == blockRow[foodCount2]) ||
(foodCol[foodCount] == blockCol[foodCount2]+1 && foodRow[foodCount] == blockRow[foodCount2]) ||
(foodCol[foodCount] == blockCol[foodCount2]-1 && foodRow[foodCount] == blockRow[foodCount2]) ||
(foodCol[foodCount] == blockCol[foodCount2] && foodRow[foodCount] == blockRow[foodCount2]+1) ||
(foodCol[foodCount] == blockCol[foodCount2] && foodRow[foodCount] == blockRow[foodCount2]-1) ||
(foodRow[foodCount] == 1 && foodCol[foodCount] == 1) ||
(foodRow[foodCount] == 2 && foodCol[foodCount] == 1) ||
(foodRow[foodCount] == 1 && foodCol[foodCount] == 2) ||
(foodRow[foodCount] == 2 && foodCol[foodCount] == 2)){
foodNum2 = (rand()%9)+1;
shuffle(&foodCol[foodCount2], &foodCol[foodNum2]);
shuffle(&foodRow[foodCount2], &foodRow[foodNum2]);
}
}
}
Remarks: the shuffle function is just a basic function of shuffling 1d arrays. Also, the foodcount and randdim are irrelevant to my problem.
Thanks!

Simplifying (and make it more legible) logic condition

I am looking for help in making this logic more legible. Assume each alphabet letter is a compare statement (e.g TRUE == a.foo). Each alphabet is about 30 char long statements.
if ( ((a || b)
&& (c || d)) ||
((e || f)
&& (g || h)) )
Any suggestions?
Decompose it.
int ab = a || b,
cd = c || d,
ef = e || f,
gh = g || h,
firstThing = ab && cd,
secondThing = ef && gh;
if (firstThing || secondThing)
Try lining up the subexpressions in groups, lining up the parenthesis:
if (((a || b) && (c || d)) ||
((e || f) && (g || h)))
In order for the conditions to align properly and the logic operator to stand out, I would use this style:
if (((a || b) && (c || d))
|| ((e || f) && (g || h))
...
|| ((u || v) && (w || x))) {
/* handle the successful test */
} else {
/* handle the other cases */
}
There are a few variations of this, but here's one way that I think is pretty clear:
if
(
(
(a || b)
&&
(c || d)
)
||
(
(e || f)
&&
(g || h)
)
)

C - check matrix[x][y] positions around

I've a matrix with size 7x7 that represents a game board. When a player makes a move, the program has to check the positions around the coordinates where the piece is, in order to detect another piece aside.
I use this function:
int check_position(COORDINATES coordinates, char board[7][7]) {
int result = -1;
if (board[coordinates.x][coordinates.y] != 'O' && board[coordinates.x-1][coordinates.y] != 'O' && board[coordinates.x][coordinates.y-1] != 'O' && board[coordinates.x+1][coordinates.y] != 'O' && board[coordinates.x][coordinates.y+1] != 'O' && board[coordinates.x-1][coordinates.y-1] != 'O' && board[coordinates.x+1][coordinates.y+1] != 'O' && board[coordinates.x-1][coordinates.y+1] != 'O' && board[coordinates.x+1][coordinates.y-1] != 'O') {
result = 1;
}
return result;
}
The first parameter are the coordinates of the player's piece as a struct, with members x and y. The second parameter is the board array.
The if statement doesn't work to well, and I don't know which alternative can I take.
Can you help me? Thanks!
You forgot about your coordinates overflowing at the borders. You can either test for this, or:
Hint: Make the array two rows and columns larger than the board and fill the border with "empty" marker. The active board will the have coordinates 1...7 This way your coordinates cannot wrap (1 - 1 and 7 + 1 are still within the array) and you do not have to care about the borders.
Note: If you just want to return a boolean value, it would be better to use stdbool.h and return a bool result. That way, the caller can directly use that function as a condition:
#include <stdbool.h>
...
bool check_position(COORDINATES coordinates, const char board[9][9]) {
int x = coordinates.x - 1
for ( int xc = 0 ; xc < 3 ; xc++ ) {
int y = coodinates.y - 1;
for ( int yc = 0 ; yc < 3 ; yc++ ) {
if ( board[x][y] != '0' )
return true;
y++;
}
x++;
}
return false;
}
Note: as you only need one one non-empty field, you can terminate instantly if you found one. That is identical to the multiple conditions. Of course, that also works for your original int result.
Note2: I modified the type of board to being const, as it is not changed inside the function.
You could also solve the edge overflow like this. Edit improved after discussion with #Olaf
#define BOARD 7
int check_position(COORDINATES coordinates, char board[BOARD][BOARD]) {
int result = -1;
int left = coordinates.x == 0 ? 0 : coordinates.x - 1;
int top = coordinates.y == 0 ? 0 : coordinates.y - 1;
int right = coordinates.x == BOARD-1 ? coordinates.x : coordinates.x + 1;
int bottom = coordinates.y == BOARD-1 ? coordinates.y : coordinates.y + 1;
if (board[left] [top] != 'O' &&
board[coordinates.x][top] != 'O' &&
board[right] [top] != 'O' &&
board[left] [coordinates.y] != 'O' &&
board[coordinates.x][coordinates.y] != 'O' &&
board[right] [coordinates.y] != 'O' &&
board[left] [bottom] != 'O' &&
board[coordinates.x][bottom] != 'O' &&
board[right] [bottom] != 'O' && )
{
result = 1;
}
return result;
}

Trying to assign specific chars as a variable in C...giving me some difficulty

So I am trying to assign a char variable with the value "#" or "%" or "!" and if the variable does not have that value, I am prompting the user with an error. While compiling, I am getting the error "error: comparison between pointer and integer". Now, my code where the error is happening is this segment:
if (((a == !) || (a == %) || (a == #)) && (w > 0 && h > 0)) {
//do something
}
I can't figure out for the life of me why an error is coming up here. Any thoughts would be greatly appreciated.
Chars in C must be surrounded by single quotes:
if (((a == '!') || (a == '%') || (a == '#')) && (w > 0 && h > 0))

Resources