What is the equivalent to the condition:
!((x > y) && (y <= 0))
Is it:
!(x > y) && !(y <= 0)
(x <= y) || (y > 0)
(x < y) || (y > 0)
After initializing the variables to make them into y = -3 and x = -3 I was able to isolate only the second one to display the same results as the first. Does this mean that this is the only one equivalent to it?
How exactly do you use truth tables or the "De Morgan's Law"?
I believe its the second one : (x <= y) || (y>0)
Related
I am displaying a card stack and need to know the exact pinpoints of the pixels so I mapped out a function that should return whatever LMB location was at pressed time. The issue with this is that for some reason I can no longer see the cards.
I tried making the function a class member of Game, but then the mouse logic doesn't work at all. I still had MyMouse M; declared but its like the code was just ignored completely. I know how to overload functions and yet this just seems to defy logic.
Function for MouseLogic();
void MouseLogic() {
Game G;
Coordinates C;
MyMouse M;
G.PrintLL(10, 25, G.start_user);
G.PrintLL(10, 10, G.start_cpu);
while (1) {
M.ReadMouseInput();
switch (M.InputRecord.EventType)
{
case MOUSE_EVENT: // mouse input
if (M.InputRecord.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
{
int x = M.InputRecord.Event.MouseEvent.dwMousePosition.X;
int y = M.InputRecord.Event.MouseEvent.dwMousePosition.Y;
cout << GetCardNumber(x, y);
}
}
}
}
Function for GetCardNumber
int GetCardNumber(int x, int y)
{
if ((x >= 10 && x <= 23) && (y >= 10 && y <= 25))
return 1;
else if ((x >= 41 && x <= 54) && (y >= 10 && y <= 25))
return 2;
else if ((x >= 72 && x <= 85) && (y >= 10 && y <= 25))
return 3;
else if ((x >= 45 && x <= 60) && (y >= 10 && y <= 25))
return 4;
else if ((x >= 78 && x <= 91) && (y >= 10 && y <= 25))
return 5;
else if ((x >= 10 && x <= 23) && (y >= 25 && y <= 40))
return 6;
else if ((x >= 41 && x <= 54) && (y >= 25 && y <= 40))
return 7;
else if ((x >= 72 && x <= 85) && (y >= 25 && y <= 40))
return 8;
else if ((x >= 45 && x <= 60) && (y >= 25 && y <= 40))
return 9;
else if ((x >= 78 && x <= 91) && (y >= 25 && y <= 40))
return 10;
else
return 0;
}
This seemed to work for anyone wondering! I just had to pass the class as an argument for some reason. Would still love an explanation for the logic behind this.
void MouseLogic(Game G) {
Coordinates C;
MyMouse M;
G.PrintLL(10, 25, G.start_user);
G.PrintLL(10, 10, G.start_cpu);
while (1) {
M.ReadMouseInput();
switch (M.InputRecord.EventType)
{
case MOUSE_EVENT: // mouse input
if (M.InputRecord.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
{
int x = M.InputRecord.Event.MouseEvent.dwMousePosition.X;
int y = M.InputRecord.Event.MouseEvent.dwMousePosition.Y;
cout << GetCardNumber(x, y);
}
}
}
}
I have a square matrix, 40 x 40, and a draw circle function that uses this formula.
I have another function that reads input from a file, the point itself (x0, y0) and the type of circle (0 or 1) and the radius.
void cerc(int x0, int y0, int r, int** matriceHarta, int tip, int n, int m)
{
if (r == 0)
return;
int x, y, xx, rr;
for (rr = r * r, x = -r; x <= r; x++)
for (xx = x * x, y = -r; y <= r; y++)
if (xx + (y * y) <= rr && matriceHarta[x0 + x][y0 + y] == 0)
{
if (tip == 0)
matriceHarta[x0 + x][y0 + y] = -5;
else if (tip == 1)
matriceHarta[x0 + x][y0 + y] = -6;
}
}
N and M are the rows and columns, but right now they are both equal.
The matrix is allocated dynamically and is transmitted via the int** matriceHarta parameter.
If I put the point on (39, 39) and I give it the radius 5, the program returns a negative exit code, which I found out is an out of bounds related error. I looked over the for loops and it makes sense that that'd be the error and tried to create the condition if((x0 + x) < n && (y0 + y) < m) to check the bounds, but it still gives the error.
Question is, what am I doing wrong? For contrast, point(37, 4) with radius = 2 is OK, but point(38, 4) with radius = 2 is not OK
This is the attempted fix:
for (rr = r * r, x = -r; x <= r; x++)
for (xx = x * x, y = -r; y <= r; y++)
if (xx + (y * y) <= rr && matriceHarta[x0 + x][y0 + y] == 0
&& (((x0+x) < n) && ((y0+y) < m)) )
//^^^^^ this is the condition i was talking about
{
if (tip == 0)
matriceHarta[x0 + x][y0 + y] = -5;
else if (tip == 1)
matriceHarta[x0 + x][y0 + y] = -6;
}
The issue is that you are testing for the out-of-bounds condition after you have already accessed potential out-of-bounds elements.
Let's break it down into separate lines:
if (xx + (y * y) <= rr && matriceHarta[x0 + x][y0 + y] == 0
&& // <-- This binds the conditions
(((x0+x) < n) && ((y0+y) < m)))
The line above the && marked with <-- is evaluated before the line below the <--.
In summary, the logical && is always evaluated from left-to-right, where the right side will not be evaluated if the left side evaluates to false (short-circuit boolean evaluation).
Thus the fix is to test the bounds condition first (swap the lines in the code above).
However, to make this a little more clear, you could break up the statement into two if statements:
if (x0+x < n && y0+y < m)
{
if (xx + (y * y) <= rr && matriceHarta[x0 + x][y0 + y] == 0)
{
...
}
}
Given the following code pattern, wherein I am trying to state a vector direction in increments of 45 degrees over the integers int x and int y, inside of a circle positioned at the origin
// x == 0 && y == 0 case is already taken cared of
if(x > 1) {
if(y == 0) {
// horizontal right
m_rotation = 0;
}else if(y < 1) {
// diagonal down right
m_rotation = 315;
} else if(y > 1) {
// diagonal up right
m_rotation = 45;
}
} else if(x == 0) {
if(y < 1) {
// vertical down
m_rotation = 270;
} else if(y > 1) {
// vertical up
m_rotation = 90;
}
} else if(x < 1){
if(y == 0) {
// horizontal left
m_rotation = 180;
}else if(y < 1) {
// diagonal down left
m_rotation = 225;
} else if(y > 1) {
// diagonal up left
m_rotation = 135;
}
}
I am looking for an elegant way to make this compact. I know there's the spaceship operator <=>, but I need to restrict myself to C++17.
Things I have tried
Nesting ternary operators with m_rotation = x > 1? (y < 1? (y == 0? 0: 315): 45): (x == 0? (y < 1? 270: 90): (y < 1? (y == 0? 180: 225): 135));, but this looks weird
I tried putting the x == 0 case inside x < 1 case and transform the later into else, but that does not simplify enough the code
Using absolute values to compare x and y, but I quickly get lost
Nothing else really, I don't know what else to try
Something like
constexpr int rotation[3][3] = {
{225, 180, 135},
{270, 0, 90},
{315, 0, 45},
};
if (x != 0 || y != 0) // if (!(x == 0 && y == 0))
m_rotation = rotation[1 + sign(x)][1 + sign(y)];
There is a closed form:
// standard sign functions
int xs = x < 0 ? -1 : x > 0;
int ys = y < 0 ? -1 : y > 0;
return 180 - 45 * (xs + 2) * ys + 90 * (xs * xs + xs) * (ys * ys - 1);
or shorter
return 180 * (x < 0 || y) - 45 * (xs + 2) * ys;
I am having problems making a loop which stops when both x and y are in the range/interval [0,1] in c++.
double x;
double y;
while(condition)
{
if(x < 0)
{
x = -x;
}
else
{
x = 2 - x;
}
if(y < 0)
{
y = -y;
}
else
{
y = 2 - y;
}
}
This method with 2 loops works:
while((x < 0) || (x > 1)) {do sth}
while((y < 0) || (y > 1)) {do sth}
This doesn't work:
while(!((x >= 0) && (x <= 1)) && !((y >= 0) && (y <= 1))) {do sth}
And this doesn't work either:
while(((x < 0) || (x > 1)) && ((y < 0) || (y > 1))) {do sth}
This makes an infinite loop (in my case):
while(((x < 0) || (x > 1)) || ((y < 0) || (y > 1))) {do sth}
Note: {do sth} changes x and y if needed so they will eventually go in that interval (same as in the first block of code).
Note 2: By doesn't work I mean it never goes in the loop when x is in the interval and y < 0 (and some other cases).
while ( !( (x>=0 && x<=1) && (y>=0 && y<=1) ) ) should be the combined conditional check.
I'd go for a dedicated function with a speaking name: so you can still understand your code in a couple of weeks :-), e.g.
auto check_outside_interval_0_1 = [] (double const a) {
return a < 0.0 or 1.0 < a;
};
while( check_outside_interval_0_1(x) or
check_outside_interval_0_1(y) ) {
// ... do your things here
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
My code is trying to determine if a triangle with the following side lengths (x, y, z) is an isosceles triangle.
It doesn't work, my code is :
if(x == y && y != z || y == y && y != z || x == z && z != y){
cout << "isosceles triangle" << endl;
}
first, you need to separe each case with ( ):
if((x == y && y != z) || (y == y && y != z) || (x == z && z != y) ){
so , you can have one case or another case or another case.
second:
y == y && y != z
is wrong (mathematically, not programmatically) this makes no sense for validating an isosceles triangle. the correct would be
y == z && x != z
the final result is
if((x == y && y != z) || (y == z && x != z) || (x == z && z != y) ){
I suggest using parenthesis:
if ((x == y && y != z) || (y != z) || (x == z && z!= y))
{
//...
}
I removed the expression y == y because it is always true.