Given a 2D array, convert to Z-Order - c++

Having trouble wrapping my head around this conversion. I want to recursively convert a 2D NxN matrix into its z-order version.
For example given array:
[ 1 2 ]
[ 3 4 ]
The Z-order is
[ 1 2 3 4]
What are the steps recursively for the z-order conversion?

The recursive way is simple:
visit top-left
visit top-right
visit bottom-left
visit bottom-right
In code
#include <iostream>
template<typename M, typename CBACK>
void zorder(const M& m, int y0, int x0, int size,
CBACK cback)
{
if (size == 1) {
// Base case, just one cell
cback(m[y0][x0]);
} else {
// Recurse in Z-order
int h = size/2;
zorder(m, y0, x0, h, cback); // top-left
zorder(m, y0, x0+h, h, cback); // top-right
zorder(m, y0+h, x0, h, cback); // bottom-left
zorder(m, y0+h, x0+h, h, cback); // bottom-right
}
}
void print(int x) {
std::cout << x << " ";
}
int main(int argc, const char *argv[]) {
int x[][4] = {{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12},
{13, 14, 15, 16}};
zorder(x, 0, 0, 4, print);
std::cout << std::endl;
return 0;
}
Output of this program is
1 2 5 6 3 4 7 8 9 10 13 14 11 12 15 16
Note that there is also another non-recursive approach: to visit the elements in z-order you can just iterate over a counter and take odd-bits as y and even-bits as x (counting bits from 0):
int zorder_x_of(int index) {
int x = 0;
for (int b=0,k=0; (1<<b) <= index; b+=2,k++) {
x += ((index & (1<<b)) != 0) << k;
}
return x;
}
int zorder_y_of(int index) {
return zorder_x_of(index>>1);
}
template<typename M, typename CBACK>
void zorder2(const M& m, int size, CBACK cback)
{
for (int i=0; i<size*size; i++) {
cback(m[zorder_y_of(i)][zorder_x_of(i)]);
}
}
Note:
In the above code samples I created a function that accepts a "callback" (named cback) that is something that will be called with the elements of the matrix, one at a time, in z-order.
To allow using both as matrix and as callback anything that supports double [] indexing and anything that can be called I used a C++ template.
In the main program as matrix I've used a bi-dimensional array of integers and a function, but the code would have compiled even for example with an std::vector< std::vector< double > > as matrix and an object instance of a class providing operator()(double) as callback.

A 2D matrix behaves internally as an 1D array i.e. it is already in "Z order".
Just iterate across the pointer that points to the first element up to NxM where N is the number of columns and M the number of rows.
example:
int arr[2][2] = {{2,4},{3,5}};
for (int i=0; i<2 * 2; ++i){
std::cout << *(&arr[0][0] + i); // or *(arr + i)
}

Related

Creating clean array everytime function is run in Arduino code

I'm writing a program which gets the distances from five ultrasonic sensors and uses a trilateration algorithm on every combination of distance values. It first outputs all the coordinates it recieves from the trilateration algorithm, then it filters out values which are too small or too large. However, it keeps outputting values from previous trilateration cycles as the array has not been reset.
#include "NewPing.h"
#include<stdio.h>
#include<stdlib.h>
#define TP0 11
#define EP0 13
#define TP1 3
#define EP1 4
#define TP2 6
#define EP2 2
#define TP3 5
#define EP3 7
#define TP4 10
#define EP4 8
#define MAX_DISTANCE 150
//sensor coordinates
int xcords [5] = {3, 45, 87, 87, 3};
int ycords [5] = {0, 16, 0, 58, 58};
int r = 3;
int n = 5;
float x = 0;
float y = 0;
int MAX_X = 85;
int MAX_Y = 62;
int sensor;
int count = 0;
int resultCount = 0;
bool reading = false;
void setup() {
Serial.begin (9600);
}
void
loop() {
while(count < 1){
count += 1;
int distances [3];
for (int r = 0; r<5; r++){
distances[r] = getValue(r);
if (distances[r] >= MAX_DISTANCE || distances[r] <= 2) {
getValue(r);
}
//Serial.print("Distance =");
//Serial.print(distances[r]);
//Serial.println(" cm ");
}
printCombination(distances, xcords, ycords, n, r);
//trilateration(distances[0], distances[1], distances[2]);
delay(5000);
}
}
int combinationUtil(int distances[], int data[], int dataCords[], int start, int end, int index, int r, float xresults[], float yresults[]);
// Needed for qsort. See http://w...content-available-to-author-only...s.com/reference/cstdlib/qsort/
int compare (const void * a, const void * b)
{ return ( *(int*)a - *(int*)b ); }
// The main function that prints all combinations of size r
// in arr[] of size n. This function mainly uses combinationUtil()
int printCombination(int distances[], int xcords[], int ycords[], int n, int r)
{
// A temporary array to store all combination one by one
int data[r];
int xdataCords[r];
int ydataCords[r];
float xresults[10];// the arrays causing the issue
float yresults[10];
// Sort array to handle duplicates
qsort (distances, n, sizeof(int), compare);
// Print all combination using temprary array 'data[]'
combinationUtil(distances, data, xdataCords, ydataCords, 0, n-1, 0, r, xresults, yresults);
sorting(xresults,yresults);
resultCount = 0;
Serial.print("\n");
}
/* distances[] ---> Input Array
data[] ---> Temporary array to store current combination
start & end ---> Staring and Ending indexes in distances[]
index ---> Current index in data[]
r ---> Size of a combination to be printed */
float* combinationUtil(int distances[], int data[], int xdataCords[], int ydataCords[], int start, int end, int index, int r, float xresults[], float yresults[])
{
// Current combination is ready to be printed, print it
if (index == r)
{
trilateration(data[0], data[1], data[2], xdataCords[0], xdataCords[1], xdataCords[2], ydataCords[0], ydataCords[1], ydataCords[2], xresults, yresults);
resultCount++;
}
// replace index with all possible elements. The condition
// "end-i+1 >= r-index" makes sure that including one element
// at index will make a combination with remaining elements
// at remaining positions
for (int i=start; i<=end && end-i+1 >= r-index; i++)
{
data[index] = distances[i];
xdataCords[index] = xcords[i];
ydataCords[index] = ycords[i];
combinationUtil(distances, data, xdataCords, ydataCords, i+1, end, index+1, r, xresults, yresults);
// Remove duplicates
while (distances[i] == distances[i+1])
i++;
}
}
float* trilateration(int d1,int d2, int d3, int x1, int x2, int x3, int y1, int y2, int y3, float xresults[], float yresults[]){
float va = ((sq(d2) - sq(d3)) - (sq(x2) - sq(x3)) - (sq(y2) - sq(y3))) / (2) ;
float vb = ((sq(d2) - sq(d1)) - (sq(x2) - sq(x1)) - (sq(y2) - sq(y1))) / (2) ;
y = ((vb*(x3-x2)) - (va*(x1-x2))) / (((y1-y2)*(x3-x2)) - ((y3-y2)*(x1-x2)));
x = (va - (y*(y3-y2))) / (x3-x2);
//Serial.print(d1);
//Serial.print(d2);
//Serial.print(d3);
Serial.print("The coordinates are: ");
Serial.print(abs(x));
xresults[resultCount] = abs(x);
Serial.print(", ");
Serial.println(abs(y));
yresults[resultCount] = abs(y);
//sorting(xresults, yresults);
return xresults, yresults;
}
void sorting(float xresults[], float yresults[]){
float xfinal[10];
float yfinal[10];
qsort (xresults, 10, sizeof(int), compare);
qsort (yresults, 10, sizeof(int), compare);
for (int i = 0; i<10; i++){
if (xresults[i] > 3 && xresults[i] < MAX_X){
xfinal[i] = xresults[i];
Serial.print("x=");
Serial.print(xresults[i]);
Serial.print("\n");
}
if (yresults[i] > 10 && yresults[i] < MAX_Y){
yfinal[i] = yresults[i];
Serial.print("y=");
Serial.print(yresults[i]);
Serial.print("\n");
}
}
}
float getValue(int sensor){
NewPing sonar0(TP0, EP0, MAX_DISTANCE);
NewPing sonar1(TP1, EP1, MAX_DISTANCE);
NewPing sonar2(TP2, EP2, MAX_DISTANCE);
NewPing sonar3(TP3, EP3, MAX_DISTANCE);
NewPing sonar4(TP4, EP4, MAX_DISTANCE);
switch(sensor){
case 0:
return sonar0.ping_cm();
case 1:
return sonar1.ping_cm();
case 2:
return sonar2.ping_cm();
case 3:
return sonar3.ping_cm();
case 4:
return sonar4.ping_cm();
}
}
the arrays "xresults and yresults" is the array causing the problem. You can see that I have redefined it every time printCombination is called so it should be cleared right?
This is my output:
The coordinates are: 40.33, 34.88
The coordinates are: 58.62, 8.00
The coordinates are: 42.09, 8.00
The coordinates are: nan, 7.74
The coordinates are: 59.14, 24.17
The coordinates are: 47.81, 13.41
The coordinates are: 47.81, 7.74
x=40.25
x=58.56
y=24.16
x=47.81
y=13.41
x=47.81
x=32.99
New distance call
The coordinates are: 40.65, 35.72
The coordinates are: 58.62, 8.00
The coordinates are: 42.09, 8.00
The coordinates are: nan, 7.28
The coordinates are: 60.07, 25.03
The coordinates are: 47.81, 13.41
The coordinates are: 47.81, 7.28
x=40.50
x=58.56
y=35.56
x=47.81
y=13.41
x=47.81
x=32.99
New distance call
The coordinates are: 24.98, 15.69
The coordinates are: 50.48, 8.00
The coordinates are: 112.35, 8.00
The coordinates are: nan, 25.91
The coordinates are: 14.66, 84.64
The coordinates are: 91.00, 12.26
The coordinates are: 91.00, 25.91
x=14.63
x=50.31
y=15.70
y=12.26
y=25.91
x=32.99
As you can see there are values which are not in the list of coordinates appearing in the print out after the coordinates.

Can we add an integer to an array in c++

#include <bits/stdc++.h>
using namespace std;
/*Prototype for utility functions */
void printArray(int arr[], int size);
void swap(int arr[], int fi, int si, int d);
void leftRotate(int arr[], int d, int n)
{
/* Return If number of elements to be rotated
is zero or equal to array size */
if(d == 0 || d == n)
return;
/*If number of elements to be rotated
is exactly half of array size */
if(n - d == d)
{
swap(arr, 0, n - d, d);
return;
}
/* If A is shorter*/
if(d < n - d)
{
swap(arr, 0, n - d, d);
leftRotate(arr, d, n - d);
}
else /* If B is shorter*/
{
swap(arr, 0, d, n - d);
leftRotate(arr + n - d, 2 * d - n, d); /*This is tricky*/
}
}
/*UTILITY FUNCTIONS*/
/* function to print an array */
void printArray(int arr[], int size)
{
int i;
for(i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
/*This function swaps d elements starting at index fi
with d elements starting at index si */
void swap(int arr[], int fi, int si, int d)
{
int i, temp;
for(i = 0; i < d; i++)
{
temp = arr[fi + i];
arr[fi + i] = arr[si + i];
arr[si + i] = temp;
}
}
// Driver Code
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7};
leftRotate(arr, 2, 7);
printArray(arr, 7);
return 0;
}
// This code is contributed by Rath Bhupendra
I found this code on the geek for geeks website. The code is used to rotate the elements of an array. It is mentioned as block swap algorithm in the website, my questions are:
Can we add integers to an array in c++ as given in the else part of the left rotate function while passing the arguments (arr+n-d)?
How can we add integers to an array?
I tried adding an integer to an array in an online compiler and it didn't work. But the above code works perfectly giving the desired output 34567.
The link to the website is https://www.geeksforgeeks.org/block-swap-algorithm-for-array-rotation/.
Can we add integers to an array in c++ as given in the else part of the left rotate function while passing the arguments (arr+n-d)?
How can we add integers to an array?
The answer is you can't, and that's not what's happening here.
int arr[] argument decays to a pointer to the first element of the array. It's the same as having int* arr so what you are doing in arr + n - d is simple pointer arithmetic.
The pointer will be moved n - d positions relative to the position it's at before the expression is evaluated.
Supposing the result of n - d is 4, and arr is pointing to the beginning of the array passed as an argument, that is to &arr[0] (in array notation) or arr + 0 (in pointer notation), which is where it's pointing to in its inicial state, you'll have arr + 4 or &arr[4], after the evaluation, the expression provides access to the address of index 4 (the 5th element of the array). To access the value within that address you'd use *(arr + 4) or arr[4].
On a side note I wouldn't advise the use of geeksforgeeks.com to learn C++, or any other language, for that matter, this should be done by reading a good book.
A function parameter having an array type is adjusted by the compiler to pointer to the array element type. That is these two function declarations are equivalent and declare the same one function.
void leftRotate(int arr[], int d, int n);
and
void leftRotate(int *arr, int d, int n);
You even may write for example
void leftRotate(int arr[100], int d, int n);
void leftRotate(int arr[10], int d, int n);
void leftRotate(int arr[1], int d, int n);
Again these declarations declare the function
void leftRotate(int *arr, int d, int n);
So within the function this expression
arr + n - d
uses the pointer arithmetic applied to the pointer arr.
For example the expression arr + 0 is equivalent to arr and points to the first element of the array. The expression arr + n points to the n-th element of the array.
Here is a demonstrative program where there is used the pointer arithmetic to output elements of an array in a loop.
#include <iostream>
int main()
{
int a[] = { 1, 2, 3, 4, 5 };
for ( size_t i = 0; i < sizeof( a ) / sizeof( *a ); i++ )
{
std::cout << *( a + i ) << ' ';
}
std::cout << '\n';
return 0;
}
The program output is
1 2 3 4 5
In the expression *( a + i ) the array designator a is implicitly converted to pointer to its first element.
Here is one more demonstrative program that shows that a function parameter having an array type is adjusted by the compiler to pointer to the array element type.
#include <iostream>
#include <iomanip>
#include <type_traits>
const size_t N = 100;
void f( int a[N] )
{
std::cout << "\nin function\n";
std::cout << "sizeof( a ) = " << sizeof( a ) << '\n';
std::cout << "a is a pointer " << std::boolalpha <<std:: is_same<decltype( a ), int *>::value << '\n';
}
int main()
{
int a[N];
std::cout << "In main\n";
std::cout << "sizeof( a ) = " << sizeof( a ) << '\n';
std::cout << "a is an array " << std::boolalpha <<std:: is_same<decltype( a ), int [N]>::value << '\n';
f( a );
return 0;
}
The program output is
In main
sizeof( a ) = 400
a is an array true
in function
sizeof( a ) = 8
a is a pointer true

Reshaping flat array to complex Eigen type

How can I reshape data of size 1×2N to a complex form in Eigen to a form a P×Q complex matrix, with N complex numbers, P×Q=N? In data, the real and imaginary parts are right next to each other. I would like to dynamically reshape data as the data can have different sizes. I am trying to prevent copying and just map the data to complex type.
int N = 9;
int P = 3;
int Q = 6;
float *data = new float[2*N];
for(int i = 0; i < 2*N; i++)
data[i] = i + 1; // data = {1, 2, 3, 4, ..., 17, 18};
Eigen::Map<Eigen::MatrixXcf> A(data, P, Q); // trying to have something like this.
// Desired reshaping:
// A = [
// 1 + 2i 7 + 8i 13 + 14i
// 3 + 4i 9 + 10i 15 + 16i
// 5 + 6i 11 + 12i 17 + 18i
// ]
I tried to first convert data to a complex Eigen array (to ultimately convert to MatrixXcf), which does not work either:
Eigen::Map<Eigen::ArrayXf> Arr(data, N); // this works
Eigen::Map<Eigen::ArrayXcf> Arrc(A.data(), N); // trying to map data to an Eigen complex array.
Could stride in Eigen::Map be helpful?
The simplest solution is to loop through all the elements and convert data to an array of std::complex<float> *datac = new std::complex<float>[N];. I was wondering if Eigen can map data to datac. Thanks in advance.
Here is the MCVE answer (online example) with some extra examples of how you can use the stride to get different outcomes:
#include "Eigen/Core"
#include <iostream>
#include <complex>
int main()
{
int N = 9;
int P = 3;
int Q = 6;
float *data = new float[20*N];
for(int i = 0; i < 20*N; i++)
data[i] = i + 1; // data = {1, 2, 3, 4, ..., 170, 180};
// Produces the output of the "Desired reshaping"
Eigen::Map<Eigen::MatrixXcf>
A((std::complex<float>*)(data), P, P);
std::cout << A << "\n\n";
// Produces what you originally wrote (plus a cast so it works)
Eigen::Map<Eigen::MatrixXcf>
B((std::complex<float>*)(data), P, Q);
std::cout << B << "\n\n";
// Start each column at the 10xJ position
Eigen::Map<Eigen::MatrixXcf, 0, Eigen::OuterStride<>>
C((std::complex<float>*)(data), P, Q, Eigen::OuterStride<>(10));
std::cout << C << "\n\n";
// Skip every other value
Eigen::Map<Eigen::MatrixXcf, 0, Eigen::InnerStride<>>
D((std::complex<float>*)(data), P, Q, Eigen::InnerStride<>(2));
std::cout << D << "\n\n";
delete [] data;
return 0;
}
The output is:
(1,2) (7,8) (13,14)
(3,4) (9,10) (15,16)
(5,6) (11,12) (17,18)
(1,2) (7,8) (13,14) (19,20) (25,26) (31,32)
(3,4) (9,10) (15,16) (21,22) (27,28) (33,34)
(5,6) (11,12) (17,18) (23,24) (29,30) (35,36)
(1,2) (21,22) (41,42) (61,62) (81,82) (101,102)
(3,4) (23,24) (43,44) (63,64) (83,84) (103,104)
(5,6) (25,26) (45,46) (65,66) (85,86) (105,106)
(1,2) (13,14) (25,26) (37,38) (49,50) (61,62)
(5,6) (17,18) (29,30) (41,42) (53,54) (65,66)
(9,10) (21,22) (33,34) (45,46) (57,58) (69,70)

How to perform component labelling in a .bmp(binary image) containing digits 9 4 3 6?

I have a binary BMP image of size 284x1280. The image contains the digits 9 4 3 6. I want to perform component labelling on the image and mark the labels whenever the digits occur. Initially, it is a binary image with only 0 and 1 in the 2D array (0 marked as background and 1 marked as the digits)
I tried to write a component labelling function (checking 8 neighbours) and incrementing a counter whenever I find a component labelled 1:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
int func(int w, int h, int a[][1280], int i, int j, int c)
{
if(i==h||j==w)
{
return 0;
}
if(a[i][j+1]==1)
{
a[i][j+1]=c; return func(w,h,a,i,j+1,c);
}
if(a[i+1][j]==1)
{
a[i+1][j]=c; return func(w,h,a,i+1,j,c);
}
if(a[i+1][j+1]==1)
{
a[i+1][j+1]=c; return func(w,h,a,i+1,j+1,c);
}
else
{
return 0;
}
}
unsigned char* read_bmp(char *fname, int* _w, int* _h)
{
unsigned char head[54];
FILE *f=fopen(fname,"rb");
//BMP header is 54 bytes
fread(head,1,54,f);
int w=head[18]+(((int)head[19]) << 8)+(((int)head[20]) << 16)+
(((int)head[21]) << 24);
int h=head[22]+(((int)head[23]) << 8)+(((int)head[24]) << 16)+
(((int)head[25]) << 24);
//lines are aligned on 4-byte boundary
int lineSize = (w / 8 + (w / 8) % 4);
int fileSize=lineSize * h;
unsigned char *img, *data;
img =(unsigned char*)malloc(w * h), data =(unsigned
char*)malloc(fileSize);
//skip the header
fseek(f,54,SEEK_SET);
//skip palette - two rgb quads, 8 bytes
fseek(f,8,SEEK_CUR);
//read data
fread(data,1,fileSize,f);
//decode bits
int i, j, k, rev_j;
for(j=0, rev_j=h-1;j<h;j++,rev_j--)
{
for(i=0;i<w/8;i++)
{
int fpos= j * lineSize + i, pos = rev_j * w + i * 8;
for(k=0;k<8;k++)
{
img[pos+(7-k)]=(data[fpos] >> k) & 1;
}
}
}
free(data);
*_w = w; *_h = h;
return img;
}
int main()
{
int w, h, i, j, c1=0, c2=0, c3=0, c4=0, c5=0, c6=0;
unsigned char* img=read_bmp("binary.bmp",&w,&h);
int array[h][1280];
char ch;
for(j=0;j<h;j++)
{
for(i=0;i<1280;i++)
{
array[j][i]=(int(img[j * w + i])==0);
}
}
register int c=2;
for(i=0;i<h;i++)
{
for(j=0;j<1280;j++)
{
if(array[i][j]==1)
{
array[i][j]=c;
func(w,h,array,i,j,c);
}
}
}
for(i=0;i<h;i++)
{
for(j=0;j<w;j++)
{
printf("%d",array[i][j]);
}
printf("\n");
}
return 0;
}
I am getting an array of just 0 and 2, whereas it should contain 0,2,3,4,5 labels for other digits. How to fix it?
You never increment c, hence you get stuck at label 2.
Once you fix that, you’ll notice single objects being broken up into many labels. This is because you check only 3 neighbors in your recursive function. You need to check all 8 (or 4 for 4-connected neighborhood). Yes, your recursive function must be able also to travel to the left and up to follow complex shapes.
This recursive function is very inefficient and with an object large enough it could cause a stack overflow. You could instead write a loop that propagates all along the line within the object. The best algorithms for object labeling use the union-find algorithm, I encourage you to look that up.

Garbage value getting displayed on printing 2d array using row order

I am using gcc compiler on ubuntu 16 , when I am printing value garbage value is getting displayed
#include <bits/stdc++.h>
int Arrayprint(int r, int l, unsigned int* q)
{
r = 3;
l = 4;
for (int i = 0; i < r; i++) {
for (int j = 0; j < l; j++) {
cout << *(q + sizeof(unsigned int) * (i * l + j)); //Garbage getting diplay
cout << *(q + i + j); //this working
cout << "\t";
}
}
cout << "size of unsigned int : " << sizeof(unsigned int); //4
cout << "size of int : " << sizeof(int); //4
}
int main()
{
unsigned int image[R][L] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 } };
unsigned int* q = (unsigned int*)image;
Arrayprint(R, L, q);
}
From what I can tell, you understand at a low level that the address of the ith element of an array of T is base + sizeof(T) * i. That's correct, and it's good that you know that.
However, C and C++ handle this for you already. When you say q + i or q[i], it's actually compiling that into q + sizeof(T)*i anyway (with the latter also dereferencing the result).
So when you say q[sizeof(int)*i], that's actually compiling into *(q + sizeof(int)*sizeof(int)*i), which is clearly not what you wanted.
Thus, the index in the array you actually access is off by a factor of sizeof(int) and results in an out of bounds error, which is where your strange numbers are coming from.
I am using gcc compiler on ubuntu 16 , when I am printing value
garbage value is getting displayed
Instead of trying to fix what's broken in your raw array arimethics, consider using the standard containers:
#include <iostream>
#include <array>
constexpr size_t R = 3;
constexpr size_t L = 4;
using image_t = std::array<std::array<unsigned int, L>, R>;
void Arrayprint(const image_t& q) {
// range based for loops for convenience
for(auto& row : q) { // get references to each row
for(unsigned int colval : row) { // get the column values
std::cout << colval << "\t"; // print the values
}
std::cout << "\n";
}
}
int main() {
image_t image = {{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}};
Arrayprint(image);
}
Output:
1 2 3 4
5 6 7 8
9 10 11 12