Generating gradients with arrays

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 7347 Views - Last Post: 26 January 2012 - 09:17 PM Rate Topic: -----

#1 zachisi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-January 12

Generating gradients with arrays

Posted 26 January 2012 - 03:46 PM

Hi,

I have basic programming knowledge and have so far made a good start to my first programming assignment but have now hit a snag. I can honestly say I have no idea on where to go with this and my class notes and research have been of little aid (just can't get the jist of it). By the way im writing code in C language. Im a physics nerd :smile2: deep down and programming just does not swing my way. Any help in helping me understand would be greatly appreciated (links, source material, step by step analysis/working would help a lot). Cheers guys :smile2:

Q) After declaring the array, generate a smooth transition/gradient between two colours across the 256 wide by 128 high image. Build the colour values into your code to start with, but bear in mind that the finished version will require the user to supply them as 2 sets of 3 values (red, green and blue) between 0 and 255. Range checking should be implemented to ensure that colours outside this range are rejected.

So far, the code ive got to is:
#include <stdio.h>
int main()
{
	int pixelarray[128][256];
	int x, y;
	FILE *image;                                               /* File pointer */
	
	for (x = 0; x <= 128; x++) {                              /* Nested for loops to contain zero elements */
		for (y = 0; y <= 256; y++);}         

	image = fopen("image.ppm", "w");                            /* Creates file to be written */
	fprintf(image, "P3\n");                                     /* Writes to the created file */
	fprintf(image, "# image.ppm\n");
	fprintf(image, "128 256\n");
	fprintf(image, "255\n");
	fprintf(image, "%i", pixelarray[x][y]);                    /* Writes the array into the file */
	fclose(image);                                             /* Closes the file */
	return 0;
}


Thanks :smile2:

Is This A Good Question/Topic? 0
  • +

Replies To: Generating gradients with arrays

#2 OLH064   User is offline

  • Junior bit compressor

Reputation: 20
  • View blog
  • Posts: 725
  • Joined: 06-June 11

Re: Generating gradients with arrays

Posted 26 January 2012 - 04:41 PM

You shouldn't use a lesser-than-or-equal-to (<=) on line 8 and 9, but just lesser-than (<)
By using <=, you access the 128th and 256th elements of arrays that end at the 127th and 255th elements.

You'll have to make a loop like a fixed 8&9 to write 16 correctly.

Which direction should the gradient be in?
\ this way?
or | that way?
Was This Post Helpful? 0
  • +
  • -

#3 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1749
  • View blog
  • Posts: 5,901
  • Joined: 03-August 09

Re: Generating gradients with arrays

Posted 26 January 2012 - 04:44 PM

well whats your issue? you have to tell us or we can't help you. also post any errors your getting.

in the mean time...

gradients are simply change in color with respect to a location. for instance; circular gradients changes the color with respect to distance form a point. liner gradients change the color with respect to the x or y access(or both if the gradient goes across). you could plug in almost any mathematical function you could think of(even something like sin or cos).

you may want to look at this
Was This Post Helpful? 0
  • +
  • -

#4 zachisi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-January 12

Re: Generating gradients with arrays

Posted 26 January 2012 - 05:00 PM

View PostOLH064, on 26 January 2012 - 04:41 PM, said:

You shouldn't use a lesser-than-or-equal-to (<=) on line 8 and 9, but just lesser-than (<)
By using <=, you access the 128th and 256th elements of arrays that end at the 127th and 255th elements.

You'll have to make a loop like a fixed 8&9 to write 16 correctly.

Which direction should the gradient be in?
\ this way?
or | that way?


Thanks for that, ive changed it now. The gradient should be | this way. :smile2:
Was This Post Helpful? 0
  • +
  • -

#5 OLH064   User is offline

  • Junior bit compressor

Reputation: 20
  • View blog
  • Posts: 725
  • Joined: 06-June 11

Re: Generating gradients with arrays

Posted 26 January 2012 - 05:01 PM

He wants a linear gradient, and some help understanding how to write this program.
...Or this is a "gimme te codez" instance.
You should work on getting the numbers, and then the gradient.
How should you go about getting the input?
How should you loop to get the gradient?
How many things should there be in the loop?
Spoiler

This post has been edited by OLH064: 26 January 2012 - 05:10 PM

Was This Post Helpful? 0
  • +
  • -

#6 zachisi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-January 12

Re: Generating gradients with arrays

Posted 26 January 2012 - 05:02 PM

View Postishkabible, on 26 January 2012 - 04:44 PM, said:

well whats your issue? you have to tell us or we can't help you. also post any errors your getting.

in the mean time...

gradients are simply change in color with respect to a location. for instance; circular gradients changes the color with respect to distance form a point. liner gradients change the color with respect to the x or y access(or both if the gradient goes across). you could plug in almost any mathematical function you could think of(even something like sin or cos).

you may want to look at this


The issue lies in i don't know where to go from now as i am struggling to grasp what to do. Thank you for the info :)
Was This Post Helpful? 0
  • +
  • -

#7 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1749
  • View blog
  • Posts: 5,901
  • Joined: 03-August 09

Re: Generating gradients with arrays

Posted 26 January 2012 - 05:15 PM

Then you need to identify what it is your struggling with
Was This Post Helpful? 0
  • +
  • -

#8 zachisi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-January 12

Re: Generating gradients with arrays

Posted 26 January 2012 - 05:23 PM

View Postishkabible, on 26 January 2012 - 05:15 PM, said:

Then you need to identify what it is your struggling with


Im struggling with creating the code which will create a linear gradient through the array that ive created. Any help would be great :)
Was This Post Helpful? 0
  • +
  • -

#9 OLH064   User is offline

  • Junior bit compressor

Reputation: 20
  • View blog
  • Posts: 725
  • Joined: 06-June 11

Re: Generating gradients with arrays

Posted 26 January 2012 - 05:23 PM

Well, in your loop, you'll have to go though R, G, and B separately.

You'll have to use some funky math to get the value between the two Rs, Gs, or Bs that's influenced by the Y loop/array placement.

This post has been edited by OLH064: 26 January 2012 - 05:26 PM

Was This Post Helpful? 0
  • +
  • -

#10 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1749
  • View blog
  • Posts: 5,901
  • Joined: 03-August 09

Re: Generating gradients with arrays

Posted 26 January 2012 - 06:25 PM

Quote

Im struggling with creating the code which will create a linear gradient through the array that ive created. Any help would be great :)


What have you tried? Is what you tried not working? Programing is about breaking things down into parts; which part are you having trouble with? are you having trouble devising an algorithm or implementing it(hint: that's the first step)?

This post has been edited by ishkabible: 26 January 2012 - 06:29 PM

Was This Post Helpful? 0
  • +
  • -

#11 zachisi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-January 12

Re: Generating gradients with arrays

Posted 26 January 2012 - 06:43 PM

View Postishkabible, on 26 January 2012 - 06:25 PM, said:

Quote

Im struggling with creating the code which will create a linear gradient through the array that ive created. Any help would be great :)


What have you tried? Is what you tried not working? Programing is about breaking things down into parts; which part are you having trouble with? are you having trouble devising an algorithm or implementing it(hint: that's the first step)?


Im having trouble with the actual algorithm for this, any sort of help would be great :)
Was This Post Helpful? 0
  • +
  • -

#12 OLH064   User is offline

  • Junior bit compressor

Reputation: 20
  • View blog
  • Posts: 725
  • Joined: 06-June 11

Re: Generating gradients with arrays

Posted 26 January 2012 - 06:49 PM

http://paulbourke.ne.../interpolation/
Has this neat linear interpolation function, but it interpolates between two doubles, and it takes the location as a double.

Ishkabible, could you translate
double LinearInterpolate(double y1,double y2,double mu)
{return(y1*(1-mu)+y2*mu);}

to integers?
Was This Post Helpful? 0
  • +
  • -

#13 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1749
  • View blog
  • Posts: 5,901
  • Joined: 03-August 09

Re: Generating gradients with arrays

Posted 26 January 2012 - 07:56 PM

ya, there are no divisions so it would work just fine.

better yet you could use templates...
template<class T>
T LinearInterpolate(T y1, T y2, T mu) {
   return y1 * (1 - mu) + y2 * mu;
}



now it works for any number type! gotta love me some generic code :)
Was This Post Helpful? 0
  • +
  • -

#14 OLH064   User is offline

  • Junior bit compressor

Reputation: 20
  • View blog
  • Posts: 725
  • Joined: 06-June 11

Re: Generating gradients with arrays

Posted 26 January 2012 - 08:15 PM

:plain: Yeah, mu is supposed to be between 0 and 1, and the 1-mu thing is to invert... it...

int linear(int y1, int y2, int mu, int murange)
{return (y1 * (murange - mu) + y2 * mu)/murange;} /* mu is between 0 and murange */


Somebody backup this post.

EDIT: y2 was named y1 when being passed to the function

This post has been edited by OLH064: 26 January 2012 - 08:41 PM

Was This Post Helpful? 0
  • +
  • -

#15 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1749
  • View blog
  • Posts: 5,901
  • Joined: 03-August 09

Re: Generating gradients with arrays

Posted 26 January 2012 - 08:25 PM

Quote

between 0 and 1


Oops, didn't think about that.

yes, yours appears to work the way I would expect(except you have 2 arguments named 'y1'. I renamed the second to y2). However, you do have to make sure that y1 and y2 are both divisible by murange however. if you don't you get a loss of precision.

effectively, mu and murange just act as a ratio to represent mu

This post has been edited by ishkabible: 26 January 2012 - 08:28 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2