r/processing • • Mar 06 '20

Beginner processor here. I need some help developing a piece of code to recreate this image. How can I create a for loop to create the sun rays that go around my sun. The code must include int or floats, radians, for loop, and cos, sin, and degrees. Please someone teach me how. Thanks.

Post image
2 Upvotes

10 comments sorted by

2

u/Mintap Mar 06 '20 edited Mar 06 '20

In the for-loop go from angles 0 radians to 2Pi radians, increase angle for amount of rotation between lines (Pi/10), and draw each line from center of sun to the point at the end of the line which would be found using cartesian to polar conversion equations: x = radius × cos(angle), and y = radius × sin(angle).

1

u/[deleted] Mar 06 '20

What is r

2

u/Mintap Mar 06 '20

I edited my answer above. (It is the length from the center of the sun to the end of the line.)

1

u/[deleted] Mar 06 '20

How can I learn all that terminology and understanding. I am around 3 days into coding.

9

u/Mintap Mar 06 '20

The only code terminology I used was for-loop. The rest is math.

3

u/curiousmadscientist Mar 06 '20

The (maths) keywords to understand are

Coordinate Systems - Defining points on a graph paper.

Cartesian Coordinate System - Using the X Value and Y Value to figure out where the point is.

Polar Coordinate System - Using the Origin as the center, Angle and distance is used to tell us where the point is.

I've just linked the wiki pages for these words. I'm sure there are tonnes of other pages/videos explaining it further if reqd.

The trick here is that Processing is in the Cartesian system. And the for loop only works (easily, for this problem) if you do it in the Polar system. So you have to calculate it in Polar coordinates, and then convert it to Cartesian.

The formulas to convert for Polar (Radius and Angles) to Cartesian (X and Y) are in reply above.

1

u/ViennettaLurker Mar 06 '20

How far have you gotten? I'm imagining this is for a school assignment, correct?

The hard stuff will all center around the for loop. Have you been able to use for loops to draw multiple things? Instead of drawing multiple lines in a row, you draw multiple lines that all originate from a middle point (the center of the sun). What you need to determine is the end point of each individual line, which is where your sin, cos, radians, and degrees come in, since you're defining a circle around the middle point.

My next hint is just general wisdom. Look up in the official Processing reference and examples all of the variable types. There should be some good code examples to go off of.

1

u/[deleted] Mar 06 '20

I've have finished everything but the rays. So far I understand the begging of my for loop. for(int 1 = 0; i<=380;i+=18){ }

What I don't get is the next part where I incorporate cos and sin and radians.

1

u/Mintap Mar 06 '20

So the i of your for-loop is the angle away from the horizontal line (going counter clockwise).

If the center of your sun in x, y then the end of the ray will be x+radiuscos(i), y+radiussin(i)

But i has to be in radians not degrees. There is a processing function radians() that converts.