init an array of xy coords that makes curves

J

Joe

I'm trying to initialize an array of floats that are made up of x and y
coordinates. I want to make these coordinates match a curvy pattern of
some sort if they were plotted on a 2d plane so that I don't have to
write each array index by hand. I don't care what the pattern looks
like, just so that it has some bends, curves, etc...

Anyone know how this would be generated?

// i only have something like this now which gives me a straight diag line
float x[1000];
float y[1000];


for (i = 0; i < 1000; i++) {
last += 0.5;

x = last;
y = last;
}
 
L

luserXtrog

I'm trying to initialize an array of floats that are made up of x and y
coordinates. I want to make these coordinates match a curvy pattern of
some sort if they were plotted on a 2d plane so that I don't have to
write each array index by hand. I don't care what the pattern looks
like, just so that it has some bends, curves, etc...

Anyone know how this would be generated?

// i only have something like this now which gives me a straight diag line
float x[1000];
float y[1000];

for (i = 0; i < 1000; i++) {
    last += 0.5;

    x = last;
    y = last;

}


To make a line bend, its slope needs to change.
Try replacing
y = last;
with
y = sin(last);
so the x value increments steadily, but the y value
wibbles and wobbles.
 
T

Tim Harig

I'm trying to initialize an array of floats that are made up of x and y
coordinates. I want to make these coordinates match a curvy pattern of

Is summer school already in session?
some sort if they were plotted on a 2d plane so that I don't have to
write each array index by hand. I don't care what the pattern looks
like, just so that it has some bends, curves, etc...
Anyone know how this would be generated?

You need to know what kind of shape that you want and choose an equation
for it.
float x[1000];
float y[1000];

An array of coordinate structures might be more useful:

struct coordinate {
float x;
float y;
};

array = struct coordinate array[1000];

but stick to your arrays. The equation for a circle is:

y = sqrt((power(radius, 2) - power(x, 2))

Note that there are two possible y's (on positive and one negative) for
every x. That means you really only need 500 hundred x values to give you
1000 data points. Half will give a you a radius of 250 with integer x
values:

00:53,511$ cat circle.c
#include <stdio.h>
#include <math.h>

int main() {

const double radius = 250;
/* note; an array of coordinate structs might be more descriptive
* */
double x[1000], y[1000];
int current_x = -250;
int i;

/* calculate the cooridents */
for (i = 0; i < 1000; i += 2) {

/* save the x values */
x = (double) current_x;
x[i+1] = (double) current_x;

/* calculate the y values */
y = sqrt(pow(radius, 2) - pow((double) current_x, 2));
y[i+1] = sqrt(pow(radius, 2) - pow((double) current_x, 2))
* -1;

/* move on to the next x */
current_x++;
}

/* print the coordinates */
for (i = 0; i < 1000; i += 1)
printf("%f\t%f\n", x, y);

return 0;
}

00:53,512$ gcc -lm -o circle circle.c

00:53,513$ echo '
set terminal dumb 60 60
set xrange [-275:275]
set yrange [-275:275]
plot "< ./circle" using 1:2
' | gnuplot

+------+--------+---------+--------+--------+------+
| + + "< ./circle" using 1:2 + A |
| |
| AAAAAAAAAAAAAA |
| AAA AAA |
| AAA AAA |
| AAA AAA |
| AA AA |
200 ++ AA AA ++
| AA AA |
| AA AA |
| AA AA |
| AA AA |
| AA AA |
| A A |
| AA AA |
| A A |
| AA AA |
100 ++ A A ++
| A A |
| AA AA |
| A A |
| A A |
| A A |
| A A |
| A A |
| A A |
| |
0 ++A ++
| |
| A A |
| A A |
| A A |
| A A |
| A A |
| A A |
| AA AA |
| A A |
-100 ++ A A ++
| AA AA |
| A A |
| AA AA |
| A A |
| AA AA |
| AA AA |
| AA AA |
| AA AA |
| AA AA |
-200 ++ AA AA ++
| AA AA |
| AAA AAA |
| AAA AAA |
| AAA AAA |
| AAAAAAAAAAAAAA |
| |
| + + + + + |
+------+--------+---------+--------+--------+------+
-200 -100 0 100 200


00:54,514$

You can any other equation in a similar manner:

01:08,529$ cat third.c
#include <stdio.h>
#include <math.h>

int main() {

double x[1000], y[1000];
double current_x = -2;
int i;

/* calculate the cooridents */
for (i = 0; i < 1000; i++) {
x = current_x;
y = pow(current_x, 3) - (4 * current_x);
current_x += .004;
}

/* print the coordinates */
for (i = 0; i < 1000; i++)
printf("%f\t%f\n", x, y);

return 0;
}

01:08,530$ gcc -lm -o third third.c

01:08,531$ echo '
set xrange [-4:4]
set yrange [-4:4]
set terminal dumb 60 60
plot "< ./third" using 1:2
' | gnuplot

4 ++-----+-----+------+------+-----+------+-----+-----++
+ + + + "< ./third" using 1:2 + A +
| |
| |
| |
| |
| A |
3 ++ AAA ++
| A AA |
| A A |
| AA AA |
| A A |
| A A |
| A AA |
2 ++ AA A ++
| A A |
| A AA |
| A A |
| A A |
| AA A |
| A AA |
1 ++ A A ++
| A A |
| A A |
| A AA |
| A A |
| AA A |
| A A |
0 ++ A AA A ++
| A A |
| A AA |
| A A |
| AA A |
| A A |
| A A |
-1 ++ A A ++
| AA A |
| A AA |
| A A |
| A A |
| AA A |
| A A |
-2 ++ A AA ++
| AA A |
| A A |
| A A |
| AA AA |
| A A |
| AA A |
-3 ++ AAA ++
| A |
| |
| |
| |
| |
+ + + + + + + + +
-4 ++-----+-----+------+------+-----+------+-----+-----++
-4 -3 -2 -1 0 1 2 3 4

01:08,532$
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top