animation issues with objects returning back to the screen from the left side once they are out of the right side - iphone

I was just wondering how to animate for example clouds which return back from the left side of the screen once they are outside the right side of the screen
I´ve tried with clouds.center = CGPouintMake(clouds.center.x + cloudvelocity, clouds.center.y + cloudvelocity) but I can only make them go from left to right and in a decline line.
In other words...
I have troubles making them go in a straight line from left to right and return bac to the screen from the left side of the screen once they are outside the right side of the screen.
can anyone help me?

The main thing you'll probably want to take advantage of is modular arithmetic, which is the same as asking what the remainder would be if you divided by a certain amount. So, for example:
28 mod 6 = 4
% is the integer modulus operator, but with floating point numbers you need to call the library method fmod (which takes and returns doubles) or fmodf (which takes and returns single precision floats). E.g.
NSLog(#"%0.0f", fmodf(28.0f, 6.0f));
Would log '4'.
In your case you have positions that constantly change, but you want the results to be constrained to a certain window size. Supposing it's 1024 points across, you want location 1024+n to be the same as location n, which is the same as 2048+n, 3072+n, etc. What you want to do then is to keep only the remainder when divided by 1024. So, e.g.
clouds.center = CGPointMake(fmodf(clouds.center.x + cloudvelocity, 1024.0f),
fmodf(clouds.center.y + cloudvelocity, 768.0f))
Or whatever your view dimensions are.
The first potential issue is that negative numbers don't necessarily work the way you want them to. E.g.
fmodf(-23.0f, 1024.0f));
is -23.0f (as are the related fmodfs of -(1024.0f + 23.0f), -(2048.0f + 23.0f), etc). You can handle that by checking for values less than 0 and adding 1024.0f if you get one.
If you know that your computed value can go negative, but never more than 1024.0f negative then you can just do:
clouds.center = CGPointMake(fmodf(1024.0f + clouds.center.x + cloudvelocity, 1024.0f),
fmodf(768.0f + clouds.center.y + cloudvelocity, 768.0f))
Since obviously the addition of 1024.0f will have no effect on the result if the number is positive. If you don't mind two calls to fmodf then obviously:
clouds.center = CGPointMake(
fmodf(1024.0f + fmodf(clouds.center.x + cloudvelocity, 1024.0f), 1024.0f),
fmodf(768.0f + fmodf(clouds.center.y + cloudvelocity, 768.0f), 768.0f))
Will work for any input value.
The second potential issue is that you're wrapping the centre of the cloud only. So it'll just jump from one side of the screen back to the other — you'll never have the cloud half on one side of the screen and half on the other. Assuming the cloud is less than a screen wide, possible solutions are (i) draw each cloud twice, with the second either 1024 pixels to the left if the cloud centre is greater than the screen modpoint or 1024 pixels to the right otherwise; (ii) make a virtue of it and use a conceptual wraparound area of, say, 2048 pixels rather than 1024, putting the visible part in the middle. So the cloud will go completely off screen to the right, then jump to the left without anybody being able to see it, and re-emerge from that side.

Related

Get all pixels matching color on screen

I am trying to write a program which returns all pixels which match a color.
Currently, I am using the default Pixel Search, but this only returns the first found pixel, searching from the top left to the bottom right.
Is there a way to code this?
I thought that I might be able to do a search from the top left, once a pixel is found, keep searching but with x + 1. However, it would need to search in the y direction first. I am not sure how to implement this though.
Thoughts are appreciated.

Shift key + mouse left button is not lowering the terrain. how to lower the terrain?

I want to make the ground in this place of the terrain a bit deeper a bit lower but left shift key or right shift key and the left mouse button does not change anything on the terrain.
I've been looking for an answer for hours, and I finally found it.
If you are trying to lower a piece of land that is already at 0 height, you will not be able.
You can only lower areas that are above to height 0. In short, height 0 is the minimum: you cannot go to snow values ​​due to the way the terrain is created.
To be able to make valleys then you have to select the tool "Set Height", enter a height and raise all the ground to that height with "Flatten All", or small pieces of land by dragging the mouse on the ground.
If you click "Flatten All", all the terrain will be brought to that height, losing all the changes made previously for the mountains.
However, if you have already made land and you don't want to lose it and redo it from scratch, you can use the following solution:
Export the current heightmap;
Open the image in an editor; (I'm not an expert in Photoshop or other
graphics programs, but I'm sure Photoshop has a way. If there isn't
an entry in the program, find out how to use scripts in photoshop)
Find a way to set the pixels according to a formula: maximum depth of
valleys + current value of the grayscale pixel * original height /
(original height + maximum depth of valleys);
Export the image in Raw format;
Go back to Unity and import it on the ground as you read in the link
above;
In the terrain settings set the height equal to the value you used
in the formula called "original height" + the “maximum depth of
valleys” value;
You should now have the ground as before, but higher. Set the value
in the transform of the Y position equal to "-maximum depth of
valleys" so that it is at the same height as before.
You should now be able to create valleys!
I haven't tried the process, but I've thought about it so much I'm pretty sure it will work.
Good work!

Cairo library: cairo_line_to Function

I have legacy code which use cairo library to draw png and calling following function. I could not understand the following code. I know it is weired question that took some piece of code and ask question. But great if anyone provide.
#define IMAGE_SIZE_W 1024
#define IMAGE_SIZE_H 768
#define GRAPH_MARGIN_L 48
#define GRAPH_MARGIN_R 21
#define GRAPH_MARGIN_B 27
#define GRAPH_MARGIN_T 22
#define GRAPH_SIZE_W (IMAGE_SIZE_W-(GRAPH_MARGIN_L+GRAPH_MARGIN_R))
#define GRAPH_SIZE_H (IMAGE_SIZE_H-(GRAPH_MARGIN_B+GRAPH_MARGIN_T))
#define SCALE_TO_CANVAS(v,low,high,fws,margin,a,b) (((a+b*((v-low)/(high-low)))*fws)+margin)
#define SCALE_TO_CANVAS_Y(v,low,high) SCALE_TO_CANVAS(v,low,high,GRAPH_SIZE_H,GRAPH_MARGIN_T,1,-1)
#define SCALE_TO_CANVAS_X(v,low,high) SCALE_TO_CANVAS(v,low,high,GRAPH_SIZE_W,GRAPH_MARGIN_L,0,1)
void line_to_point(cairo_t *cr,float x, float y){
cairo_line_to(cr,x,y);
}
void move_to_point(cairo_t *cr,float x, float y){
cairo_move_to(cr,x,y);
}
We have data in x and y and ploting x and y. The caller function is
/* counter number of x and y row
for(i = 0; i< counter; i++)
{
move_to_point(cr,SCALE_TO_CANVAS_X(xvals[i-1],lowX,highX),SCALE_TO_CANVAS_Y(yvals[i-1],lowY,highY));
line_to_point(cr,SCALE_TO_CANVAS_X(x,lowX,highX),SCALE_TO_CANVAS_Y(y,lowY,highY));
}
Than after it calls write_png which is kind of straight forward function.
if you look, SCALE_TO_CANVAS done lot of calculation which I am not able to figure out.
cairo_line_to x and y have modified value and plot it.
I haven't looked too closely, but I would guess that your x data is between lowX and highX and your y data between lowY and highY. This data is scaled so that it it fits into the image surface (which means between 0x0 and 1024x768).
Also, this seems to add a margin on all sides (L/R/B/T would mean left, right, bottom, top).
If you want an explanation of the math used instead of just its meaning, just ask.
Edit:
Ok, we have data that describes points in a graph. The top-left corner of our input data is at position (lowX, lowY), the bottom right corner is at (highX, highY).
The area that we are drawing to has a size of 1024x768. Thus, we want to scale the input data so that it fits into this space. To make things a little more complicated, we want to keep an empty margin around the graph. This takes GRAPH_MARGIN_L pixels on the left, GRAPH_MARGIN_T pixels on the top, etc.
So the drawing area has its top-left corner at (GRAPH_MARGIN_L, GRAPH_MARGIN_T) and its bottom right corner at (1024-GRAPH_MARGIN_R, 768-GRAPH_MARGIN_B). So we are looking for a formula that maps (lowX, lowY) to (GRAPH_MARGIN_L, GRAPH_MARGIN_T) and (highX, highY) to (1024-GRAPH_MARGIN_R, 768-GRAPH_MARGIN_B).
In the following, let's just look at one of the two coordinates of the point, e.g. the x coordinate. For a point x, we first calculate a percentage that describes how far along the axis it is. 0% should mean "at the top/left end", 100% is "at the bottom/right end". The available space begins at lowX and goes to highX, so there are highX-lowX possible values. Thus, x-lowX (so that "top/left" really is at zero) falls somewhere into this range and (x-lowX)*100/(highX-lowX) is the percentage that we are looking for.
This percentage should now be mapped into the target area. First we multiple this by the width of the target space (and dividing by 100, so that the percentge goes away), so that we have a value that spans the possible range. Then we have to add the lowest possible value, so that instead of starting at zero and going up to the possible width (which is high-low), we have a value that is between low and high.
All together, the formula becomes smallestTargetX+(x-lowX)/(highX-lowX)*widthOfTheTargetArea, or in terms of your defines: GRAPH_MARGIN_L+(x-lowX)/(highX-lowX)*GRAPH_SIZE_W.
The additional variables a and b that are used in GRAPH_TO_CANVAS are used for "swapping directions". It seems like instead of having (0, 0) in the top-left corner and positive coordinates going to the right/bottom, your coordinate system has (0, 0) in the bottom-left corner and positive coordinates go to the right/top. To handle this, the percentages calculated above are multiplied with -1 and 1 is added to the coordinate, to make the mapping really map the points to each others that it should. These are the last two arguments for the GRAPH_TO_CANVAS macro.
I hope this helps you understand that macros.

D3 Stacked Bar Chart outer padding

I've been working on adapting the stacked bar chart example (http://bl.ocks.org/mbostock/1134768). The problem I'm having is that there's
always outerpadding. The API lists the outer padding as a 3rd option, but omitting
it or setting it to 0 still leaves some padding. In most cases, it isn't too bad,
but with large data sets it tends to be a huge amount of padding. For all the code
relevant to my issue, you can check the link above. It's not very noticeable in that
example, but the first bar isn't drawn until about 12 pixels (in larger data sets I'm using
this can be at 100 or more pixels); I want it to start at 0 pixels.
Thanks! If you need any more explanation just let me know and I'll do my best.
EDIT: After testing, it appears rangeBands() starts at 0, but I'm still not sure why the rounding
from round bands would round as much as it did. Oh well, I can deal with using rangeBands.

Reverse Integer Values

I have a scenario which I think I can convey giving PC Monitor example (not real though).
Assume I have two monitors both of different resolution and properties. One monitor draws mouse cursor on screen from top (0) to bottom (max-value) and other draws mouse from bottom (0) to top (max-value). In other words both have reverse y-axis of each other in drawing mouse cursor and I need to write a formula that will convert one monitor cursor position to another and vice versa given one monitor x and y cursor positions.
What formula is the best suited for this?
right_x = (1 - left_x / left_width) * right_width
right_y = (1 - left_y / left_height) * right_height
The left_x and left_y (as well as the resolutions for each display) would need to be known.