Confused in how to print an array hellically - c

I have been trying to figure out how can we print a array hellically but i am stuck on how to get started.Any algorithms or ideas will be very helpful.Thanks
HELLICALLY means printing the array in concentric circular shape

If I'm interpreting what you're saying correctly, you want to print the contents of an array, but in a spiral.
I would start by allocating a big rectangular block of memory (a 2-D array) and initializing it to zero. This represents the screen. Then devise a function for determining the coordinates of the next point in the circle and make some coordinate variables initialized to the origin point. Fill the screen by dropping array members wherever they go.
Print out the screen-array, one row at a time, but substitute space for zero.
Screen size and next-coordinate-function are left as exercises for the reader.

Related

what is the conventional way to represent snake when coding snake game in C?

A bit of context:
I have some experience with other programing languages, but when it comes to C my knowledge is not that big. I'm also attempting to make the snake game without relying on dynamic memory allocation which is not the part of std, as it is intended to run on a microcontroller.
I previously coded snake game in python, rust, and java and my go to approach was to store pairs of x and y coordinates in some form of dynamic list or vector. Every iteration of the game loop I would append the element to the list/vector of pairs based on the current last element and and respective dx and dy, and if the snake was not growing deleted or poped the first element of the vector/list, making the snake "move".
I was particularly fond of this approach, since it meant I'm not required to store my entire game field in a 2d array. It also was a very clean implementation in my opinion. Now in C, I have to major problems - no dynamically sized lists and ability to delete first element of the array and shifting all the elements back without iterating through all of the array.
For the first problem, I've considered either using a fixed size array with some limit which would be above reasonable snake length while keeping track of snake length separately, or using a linked list of structs which would contain a nullable pointer to itself. Latter one seems to be unnecessarily complex, while the first one seems like a very dirty fix.
For the second problem, I've considered overriding array pointer with the pointer to its second element, but while that semi worked - I'm concerned with following issues:
do I have to free the previous array pointer (a.k.a. the previous first element)
when doing something like this, I assume that the pointer to the array would keep on growing, and sooner or later it would segfault as it does not reuse the memory it already slided away from.
So I thought I should ask more experienced coders on a cleaner and more conventional ways to implement snake in C.
Thank you in advance,
There is an upper bound on the length of the snake -- the size of your 2d board. And this upper bound is very well achievable if your player is good at the game. Therefore you can preallocate an array of that size and use it as a pool for your linked-list nodes or a circular queue.
You can simplify further by using a 2d array for your board, and storing just the next/previous links within each cell where there's a snake. Then you don't need to store the x/y of each segment:
enum { TYPE_EMPTY, TYPE_FOOD, TYPE_SNAKE };
struct Cell { int type, next, prev; };
struct Cell board[width*height];
int head, tail;
This approach is easy to generalize to multiple players on the same board, multiple items or item types, etc.
There is nothing 'wrong' in storing an explicit 2d representation of your board. In fact it is a natural way to attack the problem. It allows fast checks for self-collisions, among other things. I bet your previous implementations resorted to a slow O(n) check to see if the snake self-intersects, which isn't pretty by itself.

How do I replace an array element in LabView? (2d array of pictures)

so I have a final project for a class where I need to make a video game in LabView. The issue I'm having at the moment is that I can't figure out the 'right' way to put 'yourShip.png' into the 2d array of 2d pictures at [0,0]. Every tutorial I can find basically has exactly what I have down below in the screenshot, and it makes sense to me. However, running the program quickly shows that it does nothing.
To describe the code, I have a path constant that leads to the picture, which feeds to a draw flattened pixelmap function. Up to this point I know the code works, since creating a test indicator reveals as such. However, next I try to use the replace array subset function to replace the (default blank) 2d picture at [0,0] with yourShip.png. 'screen' is a 5x5 2d array of 2d pictures. The local variable of the same name being outputted to is indeed the very same array.
My main guess with why my code doesn't work is because of the way I'm taking screen as the input variable and then outputting to it via a local variable. However, if this is wrong, I'm confused with how I should do it. All I want to do is 'spawn' the image at the correct index.
The replace array subset works quite literally, i.e. it can only replace existing elements.
If there is no element at the specified index because the array is smaller, the function will do just nothing.
I guess your array is empty, so, initialize your screen array first to a size of at least 1x1.

How to reverse an array in groups in C

I'm trying to do and make a software driver for one old device and i added bitmap support to it successfully. The problem is that all bitmaps are vertically flipped and I need somehow to flip them around.
My array has 230400 elements(for the resolution 320x240 as 320*3 colors is 960 elements per one y pixel, so 230400 in total)
I tried this one from Reversing arrays in C yesterday but today i realised that i need to switch every 960 elements from start to end to (i think) have it vertically flipped.
I also tried one array reverse guide on the internet but it reversed all elements and so colors changed and it flipped horizontally after sucessfully flipping vertically
For importing the 320x240 bitmap i used https://stackoverflow.com/a/9296467
,renamed data do BMPdata and added my piece of code to the B,G,R to R,G,B code
data[(i+y*320)] = BMPdata[i];
data[(i+y*320)+1] = BMPdata[i+1];
data[(i+y*320)+2] = BMPdata[i+2];
This piece of code sets every pixel in red(first line), green(second), blue(third) to the needed value from BMPdata array.
So i expect it to flip vertically because the one used before was not working and the second one which reverses the whole array from 0-230400 to 230400-0 changed also colors(which is logical for me) but also flipped the image horizontally so i also can't use that.
I want it to have every 960 elements switch from start to end without changing anything in them, so in example:
i have array of 5760 elements
960-1920: Test of the second line.
1920-2880: Third test
2880-3840: 4th line of text
3840-4800: Nearly the last thing.
4800-5760: Last line.
I expect it to flip like this:
0-960: Last line.
960-1920: Nearly the last thing.
1920-2880: 4th line of text
2880-3840: Third test
3840-4800: Test of the second line.
4800-5760: This is a test of the first line.
But it looks like it flipped like this with the reverse array thing:
0-960: .enil tsaL
960-1920: .gniht tsal eht ylraeN
1920-2880: txet fo enil ht4
2880-3840: tset drihT
3840-4800: .enil dnoces eht fo tseT
4800-5760: .enil tsrif eht fo tset a si sihT
There are several options. Just two of them are for example:
Instead of a 1-D byte array use a 2-D byte array with Y in the "outer" dimension and (X * colours) in the "inner" dimension. Use memcpy() to copy a whole pixel row without changing the sequence of bytes in it.
Define structs for pixel and row and define the bitmap as 1-D array of rows. You can copy a struct value (one row) by a simple assignment which will be compiled into a memcpy() behind the scene.
The beginner's way could also be to use two nested loops, one for Y and one for (X * colours). The Y loop will flip the bitmap while the X loop will keep the sequence.
I would go for the struct as it is more readable and implements what you mean. And you don't want to work on bytes, you want to work on pixels and rows.

Copying part of array to a second array in C

I'm writing an image processing code to perform a median filter with a variable sized window.
The greyscale image has been read into an array image1, and I'm trying to copy a window selection of the array into a second array window. This is easy for a fixed sized window (3x3 window shown) as you can just say:
window[1]=image1[m-((win_size-1)/2)][n-((win_size-1)/2)];
window[2]=image1[m][n-((win_size-1)/2)];
window[3]=image1[m+((win_size-1)/2)][n-((win_size-1)/2)];
window[4]=image1[m-((win_size-1)/2)][n];
window[5]=image1[m][n];
window[6]=image1[m+((win_size-1)/2)][n];
window[7]=image1[m-((win_size-1)/2)][n+((win_size-1)/2)];
window[8]=image1[m][n+((win_size-1)/2)];
window[9]=image1[m+((win_size=1)/2)][n+((win_size-1)/2)];
In MATLAB you can generalise this to any sized window easily by using a vector in the array call:
window = image1(m-((win_size-1)/2):m+((win_size-1)/2),n-((win_size-1)/2):n+((win_size-1)/2));
I can't work out a way to do this in C, can anyone help me with this please?
Solved by using nested for loops with a pre-defined int outside the loop. Assigned to 0 at start of first loop then +1 on each iteration.
You will have to dynamically allocate memory for an image, whatever image may be, for an array then add it to your array. I don't know exactly how to do it in C, but in C++ it would look something like:
image = new Image [5];

How to pass variable length float array to GPUImageFilter Shader?

I want to pass my touch points to GPUImage (iOS)
The Point can be translate to float array, the length of the array is variable length.
But I must direct the length of array in shader.
Disclaimer: not a glsl expert
AFAIk you can't have variable length arrays like what you want. This is a GLSL limitation, not GPUImage so it's not a quick fix- the work you'll be doing will be with textures or glsl, not GPUImage.
Here's another stack overflow post about glsl: GLSL indexing into uniform array with variable length
There's two solutions that could work:
1) Limit the number of points. It's reasonable to limit touches but in practice may be hard to narrow them down if there's too many. You could pass these points in to a fixed length array or as individual constants (one for each point). If you really care about scalability with the number of points this isn't a great method because in your shader you'll have to do check each of these points and perform the relevant computation, which could be expensive when performed for the entire image (again, depending on your use case). If for each pixel you're checking a distance to point, this could be too expensive.
2) Input your points in a texture. You can either have 2 1D textures with the x&y coordinates and then treat them like an array (then go to option 1), or you can create a 2D texture, all 0, and set parts to 1 where there are touches. The 2D texture can have a lower resolution than the actual screen. This method could be a lot less work for the shader if you're doing something simple like turning finger touches black.
Your choice depends largely on what you're doing with the points in the shader.

Resources