You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more
very stuck in Java / Processing.
What's the problem?
A uni assignment.. not left until the last minute or anything..... 🙁
I mis-read the assignment deadline by a month and have barely left my PC since Friday morning. 1.5 assignments down... 1.5 to go!
I have written code so that 3 small circles are spaced equally around a larger circle to answer part a.
part b.
adapt you sketch so the design drawn is of n (a small integer) circles around a larger circle. N is a parameter adjustable by the user eg. '+' or '-'
I've almost written the code so that I have n = 2 up to n = 8
I'm going to use a left mouse click to increase n by one and right mouse click as to decrease n by 1.
Can't get my head around this bit.
he code I have so far is (I'm still doing the maths for the coordinates of the various smaller circles
int b;
//colour black
int w;
//colour white
void setup () {
size (600, 600);
background (255, 255, 255);
b = 0;
w = 255;
//frameRate (1);
}void draw() {
fill (w);
stroke (w);
ellipse (width/2, height/2, 200, 200);
/*
fill (b);
ellipse (300,190,20,20);
fill (b);
ellipse (300,410,20,20);// above is for 2 ellipses
fill (b);
ellipse (300, 190, 20, 20);
fill (b);
ellipse (395.2627944, 355, 20, 20);
fill (b);
ellipse (204.7372056, 355, 20, 20);//above is for 3 ellipses
fill (b);
ellipse (300, 190, 20, 20);
fill (b);
ellipse (410,300,20,20);
fill (b);
ellipse (300,410,20,20);
fill (b);
ellipse (190,300,20,20);//above is for 4 ellipses
fill (b);
ellipse (300, 190, 20, 20);
fill (b);
ellipse (404,266,20,20);
fill (b);
ellipse (364,388,20,20);
fill (b);
ellipse (235,388,20,20);
fill (b);
ellipse (195,271,20,20);//above is for 5 ellipses
fill (b);
ellipse (300, 190, 20, 20);
fill (b);
ellipse (395,245,20,20);
fill (b);
ellipse (395,355,20,20);
fill (b);
ellipse (300,410,20,20);
fill (b);
ellipse (204,355,20,20);
fill (b);
ellipse (204,245,20,20);
*/
//above is for 6 ellipsesfill (b);
ellipse (300, 190, 20, 20);
//fill (b);
//ellipse ();
//fill (b);
//ellipse ();
//fill (b);
//ellipse ();
//fill (b);
//ellipse ();
//fill (b);
//ellipse ();
//fill (b);
//ellipse ();//above is for 7 ellipses
/*
fill (b);
ellipse (300, 190, 20, 20);
fill (b);
ellipse ();
fill (b);
ellipse ();
fill (b);
ellipse ();
fill (b);
ellipse ();
fill (b);
ellipse ();
fill (b);
ellipse ();
fill (b);
ellipse ();//above is for 8 ellipses
*/}
I mis-read the assignment deadline by a month and have barely left my PC since Friday morning. 1.5 assignments down... 1.5 to go!
Any help would be enormously appreciated!
I [b]think[/b] they would expect the code to calculate the positions of the circles itself, rather than you working it out manually.
If so then you'll need the equation for a circle and a bit of trig:
http://www.mathopenref.com/coordbasiccircle.html
Hmm.. I think I would not be happy with hard coding the coordinates like that...
Presumably if you can work out on paper you can work it out in Java?
I have ZERO idea how to do that Graham.
I've been using trig to calculate it. How else could you?
I've been using
x = RxCOS(A)+Y0
y = RxSIN(A)+X0
x = x axis of centre of smaller circles
y = y axis of centre of smaller circles
r = radius of the two circles (the guide circle and the smaller circle touching the outside)
A = angle off of the x axis eg. for a circle at the bottom, the angle is 180-90=90
Y0 = the y coordinate of the guide circle
X0 = the x coordinate of the guide circle
edit:
molgrips - you wouldn't be happy if you were me here about to go mental or if you were marking the code?
funkynick - No idea how to get the machine to work it out for me. Like I said, I have a very limited time so doing it the way I know!
Can't really help with the Java bit... but expicitly defining all the circles like that seems to me to be missing the point somewhat, where you are doing the maths externally to define the circles.
Wouldn't it be better to let the computer do the maths to calculate the circle coordinates as part of the routine? That way you are not limiting the number of circles... also it then becomes easy to change the starting position etc...
For the mouse clicky thing... [url= http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html ]Oracle have some tutorials on their site[/url]
Java has trig..
http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html
You'll need a method that takes at least n as a parameter.
molgrips - you wouldn't be happy if you were me here about to go mental or if you were marking the code?
Either!
x = RxCOS(A)+Y0
y = RxSIN(A)+X0
double x = r * cos(a) + y0;
double y = r * sin(a) + x0;
Is that what you are having trouble with?
To be honest you might have to admit defeat and explain to the teacher that you've cocked up the dates. Otherwise you risk producing garbage and getting a shit mark. You've got a lot of studying to do I think, don't think you can do it in the time.
Okay, so your formulae are the Parametric Equation for a Circle (plus an offset). That's fine. [url= http://www.mathopenref.com/coordparamcircle.html ]Click here to visualise it[/url].
What you need to do is work that out in code, not on paper.
Probably using the cos and sin functions from the Math library that Macken linked to above.
Molgrips - that isn't an option. I'm doing an online degree. It's my third time doing the first year. There have been (approved) mitigating circumstances for the previous 2 times but I can't f up again!
You seem to know what you're talking about. Would this work?
I create variables each for a different ellipses. I haven't worked it all out yet but, for example, 6 ellipses can make n=2, n=3, n=4
something like
int c1, c2, c3, c4, c5, c6;
void setup() {
size (600,600);
background (255,255,255);
c1 = ellipse (300, 190, 20, 20);
c2 = ellipse (300, 410, 20, 20);//etc
}
void draw() {
if '+' pressed, show c1, c2
if '+' pressed again, show c1, c3, c4
if '+' pressed again, show c1, c2, c5, c6
}
I don't know if I'd be able to get the trig into the code for the ellipses this way.
Thanks a lot
Wha....? 😕 No.
You have a variable that is how many small circles you want to draw. Lets call it NumCircles.
So you need to do a little loop to draw this many circles. Can't be arsed thinking about Java syntax off the top of my head so here is some pseudo-code:
[code]CONST AnglePerCircle = (360 / NumCircles)
FOR I = 0 to NumCircles:
angle = I * AnglePerCicle
x = r * Math.cos(angle) + y0
y = r * Math.sin(angle) + x0
ellipse (x, y, 20, 20)
END FOR[/code]
What GrahamS says is what the marking scheme will be looking for this is a prime example of using a for loop where it should be used.
For circle i of n circles
draw circle
increment i
loop
So follow the help of the guys above here and you will get where you need to. Also remember that ALL developers use [url= http://stackoverflow.com/ ]Stackoverflow[/url] so the answers you are searching for will no doubt already be out there somewhere.
Is there no way to 'call' a shape then Graham?
something like
int value, b, c1, c2, c3, c4, c5, c6;
void setup () {
size (600, 600);
background (255, 255, 255);
b = 0;
c1 = ellipse (300, 190, 20, 20);
c2 = ellipse (300, 410, 20, 20);
c3 = ellipse (395, 355, 20, 20);
c4 = ellipse (204, 355, 20, 20);
c5 = ellipse (410, 300, 20, 20);
c6 = ellipse (190, 300, 20, 20);
value = 0}
void draw() {
if (value == 0 {
c1
c2} else if (value == 0) {
c1;
c3;
c4;//etc
void mousePressed () {
if value = value +1 {
if (value > 8) {
value = 0;
}
}
Thanks
StackOverflow have policies about answering homework questions.
Generally if it sounds like homework or coursework then folk there will try to lead you to the answer yourself rather than just giving you it.
what is the subject you are studying?
[quote=makecoldplayhistory ]Is there no way to 'call' a shape then Graham?
something like...
You still seem to be ignoring the suggestions that you should define your circles using a loop and trig rather than hard code them (getting the computer to do the calcs you're doing by hand - being able to do that translation is one of the principle skills you need to learn). I don't know about your assessment, but all those "magic numbers" in your code would break any coding standards I've come across and be sent back to be recoded.
What exactly do you mean by "call a 'shape'" anyway?
Is there no way to 'call' a shape then Graham?
Short answer: no.
Long answer: yes, but it is more involved than that and not what you need to do to solve this.
You sound like you are confusing yourself a bit so I'll try to clarify. When you write:
[code]int value, b, c1, c2, c3, c4, c5, c6;[/code]
You are declaring that these variables will all hold integer values (i.e. whole numbers).
When you say:
[code]c1 = ellipse (300, 190, 20, 20);[/code]
You are NOT saying [i]"set c1 to this ellipse function"[/i]
You are saying [i]"call the ellipse function right now and set c1 to be the integer that the function returns when it is finished"[/i]
I'm assuming that ellipse() is function which draws an ellipse at the given x,y position with the given horizontal and vertical radius, yes? In which case I doubt it actually returns any value (i.e. it has a return type of "void")
What they're looking for you to achieve is simple and elegant code, not the fudge you've come up with (no offence intended!).
Break it into steps:
1. Create a method that draws a circle which takes parameters for size, x translation and y translation.
2. In your draw() method, use something like a for loop from 0 to n. The only thing that needs to change in this loop is the angular distance between circles, and that's 180 / n.
3. Use this loop to call your drawCircle(size, xLoc, yLoc) method to draw the circles around the centre point.
Is this course a single module for a humanities degree, or part of a Comp Sci syllabus?
Graham - thanks for your help. It's started to clear things up for me a little.
aracer - It's not that I'm ignoring what I've been told about loops, it's that I was trying to implement what I'd already done.
By call a shape, I meant, define a shape with x co-ord,y co-ord,width,height and then, later on in the code, something like
if z is pressed, display these shapes
if m is pressed, display these different shapes
As Graham's last post explained,it doesn't work the way I thought.
Llama - BSc Computer Science.
The four courses for this year are;
1. Intro to computing
2. Computing and the Internet
3. Maths for IT
4. Image, Sound and Motion
1. = some essays. Von Neumann, Explain IEEE 754 floating point etc.
2. = not too dis-similar to 1. Eassays about researchable facts.
3. = not my strong suit but getting there with practice.
4. = some very hard. some very, very, very hard. When you're studying with nothing more than a reading list, trying to understand coding, it's terrible!
edit:
flaperon.
part of Comp Sci. A very difficult part!
I'll look at that loop. Thanks
Ah, I understand what you mean now with Graham's clarification - you could do that using macros, but that's not what you want to do, so I won't explain how!
graham gave you some pretty good pseudo code up there - I'm glad he CBA to think about Java syntax, as otherwise he'd have written your assignment for you, and that really would be missing the point. Pretty much all you need to do though is code what he wrote in Jave and add a little bit extra for increasing and decreasing the number of circles.
[url= http://www.codecademy.com/ ]Here[/url] is a cool website. Learning how to do these things from scratch from a book with no one to help is no fun.
Graham's pseudocode above tells you exactly how to perform this task though. If you find yourself writing out lines and lines of the same code, you should be using a loop. If you find yourself doing sums and hard coding numbers then the computer should be doing the sums for you - that is what they're for 🙂
Best of luck. And don't give up, when I started my CS degree I didn't have a clue (Just started it because I liked computer games). Now I'm doing a CS PhD and teach second year undergrads how to program C.
One more question, that really will show my noviceness,
I'm using Processing 2.1 and it says Java at the top.
Is the code I'm using java language? At the moment, with my googling, I'm searching for, for example "processing create loop". Could I be looking for, "java create a loop and listen to us"?
Thanks
edit: thanks chambord. I wish I had someone teaching me.
I went to uni the 'normal way' in 2001 and studied language and linguistics.
I underestimated how hard distance learning can be!
Right, now with learning about loops!
Thanks all for your help.
Sorry I wasn't suggesting posting the question on stackoverflow, more using the existing information on there to help answer the question.
I imagine the examiners are looking for you to produce code that will draw arbitary numbers of circles on the screen, not just 3 to 8...
If you step away from the terminal for a bit and think about what you are trying to achieve as a maths problem, or even imagine how you would draw that yourself by hand, it might help. So, lets say you want to draw four circles.
First thing you are going to need to do is determine where to draw the circles. Going waaay back into my past when I taught IT stuff, we used a language called Logo (remember the turtle thing?). Okay - we want to pointer to go to four locations in a circle - we want the pointer to turn all the way around (360º) and draw four straight line between the points - a square. (okay, in the assignment, you won't draw the straight lines but at each point in the square, you will draw a circle)
Right then - so four sides, 360 is all the way around so that's 360/4 = 90º turn each time...
REPEAT 4 [FORWARD 100 RIGHT 90 (something here to draw a circle at this point)]
That would draw a square. (you're going to need to translate this Logo into Java, obviously!! 🙂 )
BUT - what if I want more circles?
Well, you still need to go all the away around, but you need to stop more times to draw a circle...
So, the amount you turn each time is less - as it happens, all the way around, divided by the number of times you want to stop off and draw a circle...
REPEAT 6 [FORWARD 100 RIGHT 60 (something here to draw a circle at this point)]
Or, to make it more readable, put in your working out...
REPEAT 6 [FORWARD 100 RIGHT 360/6 (something here to draw a circle at this point)]
This also works for 7...
REPEAT 7 [FORWARD 100 RIGHT 360/7 (something here to draw a circle at this point)]
and 23 (just as it's hard to do the maths in your head!)
REPEAT 23 [FORWARD 100 RIGHT 360/23 (something here to draw a circle at this point)]
That's the maths behind the assignment. Now make it Java 🙂
edit - and that exercise was aimed as Key Stage 3 kids and would take about an hour.
Rachel
If you are a beginner it's a good idea to find something similar and hack about the code, seeing how your changes affect the program.
This might be a start...
http://mathbits.com/MathBits/Java/Graphics/Repetitive.htm
I gtfy http://en.wikipedia.org/wiki/Processing_(programming_language) and whilst it seems it isn't quite java, the underlying syntax is the same, and you'll certainly find a lot more info if you look up java stuff.
we used a language called Logo
*Remembers many days spent using that and sheds a small tear*
[quote=allthegear ]Right then - so four sides, 360 is all the way around so that's 360/4 = 90º turn each time...
REPEAT 4 [FORWARD 100 RIGHT 90 (something here to draw a circle at this point)]
That would draw a square. (you're going to need to translate this Logo into Java, obviously!! )...
Nice logo code, but you don't quite need to do the same thing in java (or processing which appears to be much the same thing) - I'd suggest the OP uses graham's pseudo code rather than yours (not saying yours is wrong, just trying to help out).
Surprised they did not introduce the control structures (if/switch/while/for...) to you in a nice way before giving you this assignment. Next time don't just look at the reading list, try some practical examples; you can learn from a book but you need to put it in practice.
You need to have this pretty well understood or you'll have no chance once they move you onto real programming.
If you're not familiar with Java loops, download and have a play with Peter Millican's Turtle Java (version 7.x.x, not the newer 10.x).
Break the problem down into chunks. You could start with creating a Java "Circle" class which knows how to draw a circle of a supplied radius at a supplied x/y co-ordinate location.
Once you've got this working then you can create multiple Circle objects using calculated x/y + radius values.
[quote=allthepies ]Break the problem down into chunks. You could start with creating a Java "Circle" class which knows how to draw a circle of a supplied radius at a supplied x/y co-ordinate location.
He's already got that, as it's one of the add ons he gets with processing.
A java "for" loop:
[code]
int radius = 23;
int someOtherValue = -35;
for (int i=0; i<180; i++) {
// call a method you've defined somewhere else:
drawMyCircle("mogrim is the best!",i, radius, someOtherValue);
}
[/code]
If something as basic (sorry!) as a loop in Java is giving you trouble you really ought to read these:
http://en.wikipedia.org/wiki/For_loop
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html
Edit: the drawMyCircle method may not be what you need, this is an example. The loop goes up to 179, again this might not be what you want either...
You could start with creating a Java "Circle" class which knows how to draw a circle of a supplied radius at a supplied x/y co-ordinate location.
He's got that. He is using a function/method called "ellipse" that draws an ellipse at an x,y coord.
Once you've got this working then you can create multiple Circle objects using calculated x/y + radius values.
You're being all object-oriented about this. Which is fine, but at this level they'll just expect a simple imperative program, nothing enterprisey.
aracer - yes, I'm very aware of the differences (though I must admit I've not used Java in about 5 years). I just wanted to get the OP thinking about where things need to be drawn in code rather than by hand. The idea of considering a full turn and drawing circles as you go at 360/n degrees is valid in both.
Rachel
What course is it?
I suspect this 'processing' thing might be some kind of teaching environment that's not plain Java.
EDIT it's a language:
Appears to be not at all object orientated...
Appears to be not at all object orientated...
It's all about template metaprogramming and functional languages these days. OO is soooooo 2009.
Let's not freak out the OP toooo much eh? 😀
I installed processing to give it a try and it's quite neat. Pretty much standard java to the level required for projects like this (actually I'm fairly sure what I wrote would compile as C or C++, it's just standard C style stuff). Grahams pseudo code works fine.
...though as you'd expect with java sin and cos work in radians, but there is a handy TWO_PI constant.
Appears to be not at all object orientated...
I did some work with processing when I was at uni and it might as well be Java. It's super easy to get up and running and start bashing code out while totally forgetting that you should be writing OO code. Because it's essentially Java you can write classes and do everything that you would in Java, but if you don't want to, you don't need to.
We used it to bash out some simple video game style game loops with simple physics engines we'd written, all pretty easy if you've got a little coding background already.
How are you getting on OP? Did you manage to get your head around loops?
Yes and no, whatnobeer.
Whilst I understand loops and their three variables, and an write a loops that displays grids and rows and columns, I can't see how to get the formula into the loop.
I know
x = 110*cos(360/number of circles)+300
y = 110*sin(360/number of circles)+300
I have the incomplete code and know the formulas have to go in to the draw loop. That's where I'm stuck. I'm in Thailand so went to bed, dreamt about being stuck on maths and am back at it now... waiting for a lightbulb moment! 🙁
void setup () {
size (600, 600);
background (255, 255, 255);
smooth ();
ellipse (width/2, height/2, 200, 200); // the guide circle}
void draw() {
for (int x = ; x < ; x ) {
for (int y = ; y < ; y ) {ellipse (x , y , 20, 20);
}
}
}
Am I going to need several loops
One loop for two circles, one for 3 and so on to 10?
Graham. In your pseudo code, you wrote
Constant AnglePerCircle = (360/NumCircles)
How does that figure into the loops?
Thanks for any help / pointers
I have got this far.
int x, y;
void setup () {
size (600, 600);
background (255, 255, 255);
smooth ();
//ellipse (width/2, height/2, 200, 200); // the guide circle.}
void draw() {
for (int x = 110*Math.cos(180)+300; x <= 2; x ++) {
for (int y = 110*Math.sin(180)+300; y < 2; y ++) {ellipse (x*2, y*2, 20, 20);
}
}
}
I get the error message cannot convert from double to int.
Am I along the right lines?
Am I going to need several loopsOne loop for two circles, one for 3 and so on to 10?
No, one loop should do it. It should loop from 1 to n, where n is the number of circles you want to draw. Inside the loop you want to have a method that can calculate the position of the circle and draw it. You'll probably want a couple of helper methods to calculate the x and y coordinates of the circle you're drawing. Something like:
int numCircles = ...
float anglePerCircle = (360/NumCircles)
for i while i < numCirles
calculateXPos
calculateyPos
drawCirle
Edit: be careful you supply the inputs to the sin and cos functions the right way. They expect radians. Have a look at the processing reference for conversion methods
Thanks whatnobeer.
Will
int numCircles = ...
float anglePerCircle = (360/NumCircles)
be at the top of the code, above void setup?
Here is my (not-even-close-to-working) code. The problem I have is there are so many bits I don't get, I don't know where to begin. It's also holding me back as there are lots of small edits I need to make once I have this code written and then talk about the effects.
int NumCircles = 2;
float anglePerCircle = (360/NumCircles);
int x, y;
void setup () {
size (600, 600);
background (255, 255, 255);
smooth ();
//ellipse (width/2, height/2, 200, 200); // the guide circle.
}void draw() {
for (int x = 110*Math.cos (36)+300; x = 360; x ++) {
for (int y = 110*Math.sin (36)+300; y = 10; y ++) {ellipse (x, y, 20, 20);
}
}
}
I (successfully) wrote other code last night to answer another question and understand it.
I've been wondering if I can use similar code to complete this task. Creating images of each particular circle. I get why I should be using FOR loops but can't seem to understand how to use them in this case. I've read everything I can find.
I managed to write the code below by getting something to work and then adjusting it so it worked correctly. With this FOR loop, I haven't got any where near it working so can't begin to improve it.
PImage imgone;
PImage imgtwo;
int value = 0;
void setup () {
size (1000, 480);
imgone=loadImage ("vase1.jpg");
imgtwo=loadImage ("vase2.jpg");
}void draw () {
if (value == 0) {
background(0, 0, 0);
image(imgone, 0, 0);} else if (value == 1) {
background(255, 255, 255);
image(imgtwo, 0, 0);
}
}void mousePressed() {
value = value + 1;
if (value > 1) {
value = 0;
}
}
Thanks for your help. It's very much appreciated whatnobeer.
[b]edit:[/b]
I see I have to loop on a circle counter and not the co-ords. Can't see how that happens either though.
Something like
for (int circleNo = 0; circleNo < MaxCircles; circleNo ++)
Where do I implement the co-ords (written in pseudo code by Graham) into the code?
Mike
I would step back and work out exactly what needs to be done at each step to get where you need. Write out method stubs and fill them in later. For example, to draw a circle you need the x and y coordinates. How do you get them? Write a method to calculate them.
Think about what the position of the coordinates depends on and then pass those in to the method.
The loop in your edit looks ok, what do you think you should be doing in there?
I need to head off now, but try thinking it through step by step and sorting each little bit individually. I know how it feels to struggle with code at the last minute and it's not fun. But they only way you'll sort it is one step at a time.
Thanks whatnobeer (and others). I stepped back. I've written what I need to do in natural language. Tried and tried and read about and watch examples of the use of FOR loops but can't get it right.
I've had to give up on the FOR loop. I just couldn't do it.
I can use the loops for grids and rows etc but I can't even begin to understand how to use them here. The bit that confuses me is where the formulas go and how they interact with the loop.
If I use
for (int circleNo = 0; circleNo < MaxCircles; circleNo ++)
ellipse (x, y, 20, 20)
then I think I understand that the next iteration of the loop will be
for (int circleNo = 1; circleNo < MaxCircles; circleNo ++)
ellipse (x, y, 20, 20)
but I don't see how the x and y will change?
with the formulas, the variable is a.
a = 360/n (where n is the number of circles around the circle)
In pseudo code, in my mind, I need something that says
for (int circleNo = 0; circleNo < MaxCircles; circleNo ++)
ellipse (110*cos(CircleNo/360)+300, 110*sin(CircleNo/360)+300, 20, 20)
The closest I got was
int CircleNo = 0;
int MaxCircles = 10;
int MinCircles = 2;void setup () {
size (600, 600);
background (255, 255, 255);
smooth ();
//ellipse (width/2, height/2, 200, 200); // the guide circle.
}void draw() {
for (int i = 1; i < CircleNo; i = i+1) {
for (int j = 0; j < 20; j = j+1) {
// in the FOR loop, the first parameter "how does this variable start",
// then it defines what it wants to reach "when does it finish",
// third, how does the count variable update itself; here i = i+1 meaning i will add 1 to itself
ellipse (i * 20, j * 20, 20, 20);
// the ellipse will be drawn *20 on the x axis (i) and *20 on the y axis (j)
}
}
}
I understand no one's here to do my work for me, but I'm not going to understand what goes where until I've seen it. Am I close enough for someone to get me to the finish line? Many thanks in case anyone does take pity on me!
The code I have written, because an inelegant solution is better than nothing (eg. ducttape)
int value = 0;
void setup () {
size (1000, 480);
}void draw () {
if (value == 0) {
background(0, 0, 0);
ellipse (300, 190, 20, 20);
ellipse (300, 410, 20, 20); //2 circles show to begin
} else if (value == 1) {
background(0, 0, 0);
ellipse (300, 190, 20, 20);
ellipse (395, 355, 20, 20);
ellipse (204, 355, 20, 20); //3 circles
} else if (value == 2) {
background (0, 0, 0);
ellipse (300, 190, 20, 20);
ellipse (410, 300, 20, 20);
ellipse (300, 410, 20, 20);
ellipse (190, 300, 20, 20); //4 circles
} else if (value == 3) {
background (0, 0, 0);
ellipse (300, 190, 20, 20);
ellipse (404, 266, 20, 20);
ellipse (364, 388, 20, 20);
ellipse (235, 388, 20, 20);
ellipse (195, 271, 20, 20); //5 circles
} else if (value == 4) {
background (0, 0, 0);
ellipse (300, 190, 20, 20);
ellipse (395, 245, 20, 20);
ellipse (395, 355, 20, 20);
ellipse (300, 410, 20, 20);
ellipse (204, 355, 20, 20);
ellipse (204, 245, 20, 20); //6 circles
} else if (value == 5) {
background (0, 0, 0);
ellipse (300, 190, 20, 20);
ellipse (386, 231, 20, 20);
ellipse (407, 324, 20, 20);
ellipse (348, 399, 20, 20);
ellipse (252, 399, 20, 20);
ellipse (192, 324, 20, 20);
ellipse (214, 231, 20, 20); //7 circles
} else if (value == 6) {
background (0, 0, 0);
ellipse (300, 190, 20, 20);
ellipse (378, 222, 20, 20);
ellipse (410, 300, 20, 20);
ellipse (378, 378, 20, 20);
ellipse (300, 410, 20, 20);
ellipse (222, 378, 20, 20);
ellipse (190, 300, 20, 20);
ellipse (222, 222, 20, 20);
}
}void mousePressed() {
value = value + 1;if (value > 6) {
value = 0;
}
}
Am I getting closer or further away from achieving it with loops? It's still not even working enough for me to finish it off / improve it! 🙁
Close enough for someone to finish it yet? Yes, I'm desperate!
int i, j;
int CircleNo = 0;
int MaxCircles = 10;
int MinCircles = 2;
float x = (float)(110 * Cos(CircleNo/360 * Math.PI / 180F)) + 300;
float y = (float)(110 * Sin(CircleNo/360 * Math.PI / 180F)) + 300;
void setup () {
size (600, 600);
background (255, 255, 255);
smooth ();}
void draw() {
for (int i = 1; i < CircleNo; i = i+1) {
for (int j = 0; j < 20; j = j+1) {
ellipse (i * 20, j * 20, 20, 20);
}
}
}
for (int i = 1; i < CircleNo; i = i+1) {
You've set CircleNo to zero, so that won't do anything.
I can't comment on the coding but I'd wonder if rather than using the parametric equation of a circle couldn't you do this with the pythagorean form which is simpler and more elegant? (is posted in the the link above)
x^2 + y^2 = r^2
thanks oldnpastit. Changed that.
The error I get is
"the function Cos(double) does not exist"
"the function Sin(double) does not exist"
Like I said, the code's so far away from being correct that I can't see the errors in it!
From 1 - 10, how close is the code from being correct, do you know?
Thaks
No idea if you're still at it, but here goes.
"the function Sin(double) does not exist"
[url= https://processing.org/reference/sin_.html ]sin()[/url] - the method names are case sensitive and take floats, not doubles. You need to make sure your inputs are of the right type. You should either make sure they're the correct type from the start or you could try and cast them into the right type ([url= https://www.google.ca/search?q=java+how+to+cast+types&ie=utf-8&oe=utf-8&gws_rd=cr&ei=x8O0VOemMorToASd1IHIBw#q=java+cast+double+to+float ]casting doubles to floats[/url])
From looking at the ducttape code, that's what you need to do, but with a loop instead. You've hit the nail on the head when you said "but I don't see how the x and y will change?" Obviously, with the code you wrote above that, they wont. You need to think about what the x and y actually are and how they change with respect to the circle you are drawing.
float x = (float)(110 * Cos(CircleNo/360 * Math.PI / 180F)) + 300;
I would scrap this and look at using the parametric equation as posted by toys. Or [url= http://stackoverflow.com/questions/839899/how-do-i-calculate-a-point-on-a-circle-s-circumference ]read this on how to calculate x and y on the circumference of a circle. [/url]
One last hint: Each time you increment you're loop, you draw a new circle in a new position (ie new x, y). That depends on how many circles you've already drawn. Hopefully you'll see how you can write a method that works out the x and then the y based on how many circles in total you want and how any you've drawn already.
Ditch the duct-tape code. You arent being tested on getting something that works, you are being tested on using the various programming elements - loops, methods, variables etc
You'll get more credit for trying these but getting it slightly wrong than the method you've employed.
whatnobeer - I'm not still at it. I couldn't so used the code pasted below.
This part of the course ha a lot about perception of motion etc. I needed to discuss how the circles (when shown one at a time), appear to be moving around the circle / coming up from the bottom etc.
Mackem - I've submitted it now. Like I said, I needed to discuss the visual perceptions. In the commentry, I said,
"Whilst I was aware that “If you are writing a lot of repetitive code, you should probably use loops”, I was unable to get the code to function correctly. I attempted to create a FOR loop, whereby n circles could be shown at the correct x, y co-ordinates.
The codes’ purpose (see Code 7) was to loop on a circle counter eg.
I believed this would provide the most elegant solution. With it not proving to be fruitful, I decided that an in-elegant solution (such as Code 5) is better than none!"
I submitted my non-working code with loops so will hopefully get some marks for the correct approach. I have no idea if it was slightly wrong or infinite-monkey-infinite-typewriter-esque.
Whatnobeer - I appreciate you weren't going to do my work for me. Thanks for all of your help. If you have (another) few minutes, I'd love to see what the code [i]should[/i] have looked like.
My final code (which allowed me to comment on visual perception)
int value = 0;
void setup () {
size (600, 600);
}void draw () {
if (value == 0) {
background(0, 0, 0);
ellipse (300, 190, 20, 20);
ellipse (300, 410, 20, 20); //2 circles show to begin
} else if (value == 1) {
background(0, 0, 0);
ellipse (300, 190, 20, 20);
ellipse (395, 355, 20, 20);
ellipse (204, 355, 20, 20); //3 circles
} else if (value == 2) {
background (0, 0, 0);
ellipse (300, 190, 20, 20);
ellipse (410, 300, 20, 20);
ellipse (300, 410, 20, 20);
ellipse (190, 300, 20, 20); //4 circles
} else if (value == 3) {
background (0, 0, 0);
ellipse (300, 190, 20, 20);
ellipse (404, 266, 20, 20);
ellipse (364, 388, 20, 20);
ellipse (235, 388, 20, 20);
ellipse (195, 271, 20, 20); //5 circles
} else if (value == 4) {
background (0, 0, 0);
ellipse (300, 190, 20, 20);
ellipse (395, 245, 20, 20);
ellipse (395, 355, 20, 20);
ellipse (300, 410, 20, 20);
ellipse (204, 355, 20, 20);
ellipse (204, 245, 20, 20); //6 circles
} else if (value == 5) {
background (0, 0, 0);
ellipse (300, 190, 20, 20);
ellipse (386, 231, 20, 20);
ellipse (407, 324, 20, 20);
ellipse (348, 399, 20, 20);
ellipse (252, 399, 20, 20);
ellipse (192, 324, 20, 20);
ellipse (214, 231, 20, 20); //7 circles
} else if (value == 6) {
background (0, 0, 0);
ellipse (300, 190, 20, 20);
ellipse (378, 222, 20, 20);
ellipse (410, 300, 20, 20);
ellipse (378, 378, 20, 20);
ellipse (300, 410, 20, 20);
ellipse (222, 378, 20, 20);
ellipse (190, 300, 20, 20);
ellipse (222, 222, 20, 20); //8 circles
}
}void mousePressed() {
if (mouseButton == LEFT) {
value = value + 1;
} else if (mouseButton == RIGHT) {
value = value - 1;
}if (value > 6) {
value = 0;
}
if (value < 0) {
value = 6;
}
}
jeeez what they teaching nowadays blah blah in my day etc etc etc
Ilama: I've never been taught anything about this by anyone.
The only thing I have been told by the University is a reading list, an assignment deadline and an exam date. But, thanks for asking.
It's one of those problems that now you aren't under pressure to finish you can take the time to look at all the elements of what you wanted to do and come up with a better solution. It'll help your ability to program in the future if you do.
Toby - I have the solution. I just can't make it happen. I have found the code in another language. I simply can't translate it into Processing.
I can see how this could be easily adapted for my purpose.
size(200,200);
smooth();
background(255);int nbr_circles = 24;
float lg_diam = width * .85;
float lg_rad = lg_diam / 2;
float lg_circ = PI * lg_diam;float sm_diam = (lg_circ / nbr_circles);
float cx = width/2.0;
float cy = height/2.0;fill(0);
for (int i = 1; i <= nbr_circles; ++i) {
float angle = i * TWO_PI / nbr_circles;
float x = cx + cos(angle) * lg_rad;
float y = cy + sin(angle) * lg_rad;
ellipse(x, y, sm_diam, sm_diam);
}
I'm sure you mean well, but it's coming across as very patronising.
You needed to recalculate x and y inside the loop each time. If you look again at the pseudocode Graham posted on page 1 I'm sure you can do it. Here is some code which will draw a straight line of circles.
void draw () {
int x, y; // Declared x and y circle coordinates
int numIterations = 10;
for (int i = 0; i < numIterations; i++) // for loop executes 10 times
{ // This is where the loop body starts, so
// whatever is in the brackets will be
// run 10 times
x = 10 * i; // calculate x
y = 20 * i; // calculate y
ellipse(x, y, 5, 5); // draw ellipse
} // Loop body ends
}
Note that x and y are recalculated inside the loop body each iteration and then used as parameters for the ellipse method (I.e. the newly calculated position of the circle is used to draw the circle, then on the next iteration we calculate the position again and redraw in a new position).
Thanks Chambourd. I'll have a look at that when the rest of my assignments are finished.
I've had Graham's writing on a post-it in front of me all day.
My issue is getting it to work in processing. I don't think I'm writing the formulae in the correct format (syntax?).
Thanks again for taking the time to help.
Good luck in the PhD.
My issue is getting it to work in processing. I don't think I'm writing the formulae in the correct format (syntax?).
Do you realise if you right click the function you can find it in the reference?
lemonysam!!!
I had no idea!
Thank you!
It's little things like that that aren't taught with distance learning and make it so much harder.
Can you go to your supervisor and just explain that you've misread the dates and could you have an extra week?
P.S.
Have you drawn a picture of what you're doing? If not, do so right now. On the picture mark all the distances and angles.
[quote=makecoldplayhistory ]I can see how this could be easily adapted for my purpose.
size(200,200);
smooth();
background(255);
int nbr_circles = 24;
float lg_diam = width * .85;
float lg_rad = lg_diam / 2;
float lg_circ = PI * lg_diam;
float sm_diam = (lg_circ / nbr_circles);
float cx = width/2.0;
float cy = height/2.0;
fill(0);
for (int i = 1; i <= nbr_circles; ++i) {
float angle = i * TWO_PI / nbr_circles;
float x = cx + cos(angle) * lg_rad;
float y = cy + sin(angle) * lg_rad;
ellipse(x, y, sm_diam, sm_diam);
}
That is almost exactly the code you needed. Are you suggesting you can't translate that into processing? Because that looks like java, and processing is java - all you need to do is take that code and rearrange it into the processing specific functions (setup() and draw()). No translation needed.
The frustrating thing for us onlookers is that you got so close. You posted this before submitting I think:
[quote=makecoldplayhistory ]In pseudo code, in my mind, I need something that says
for (int circleNo = 0; circleNo < MaxCircles; circleNo ++)
ellipse (110*cos(CircleNo/360)+300, 110*sin(CircleNo/360)+300, 20, 20)
Is pretty much spot on - you just needed to replace the 360 with TWO_PI because sin and cos work in radians not degrees (oh and you need to watch your case - you have circleNo in the loop and CircleNo in the ellipse command). I think to some extent it's a lack of confidence rather than a lack of understanding which is the problem - why didn't you just try coding that, after fixing the case and adding a ; to the end of the ellipse function it should compile and work.
Edit: actually there is a mistake in the angle formula - you need
[code]360 * circleNo / MaxCircles[/code]
not
[code]circleNo / 360[/code]
(obviously replace 360 with TWO_PI as above)
[quote=toys19 ]I can't comment on the coding but I'd wonder if rather than using the parametric equation of a circle couldn't you do this with the pythagorean form which is simpler and more elegant? (is posted in the the link above)
x^2 + y^2 = r^2
Neat, but not an easy formula to use to create points evenly spaced around a circle (off the top of my head I can't think how to do that at all).
oldnpastit -
It's an online degree. I thought I'd save money and go through Goldsmiths as opposed to The OU.
I don't have a supervisor nor anyone to contact. There's no online discussion board.
I wasn't exaggerating when I said you are sent a reading list, examination dates and an assignment. This assignment (plus 2 others) was set 6 weeks ago. Due on the 15th.
I've got sketches (on several sheets of paper) from where I was working out the x,y co-ords manually.
How will they help me if it's being calculated inside Processing?
Thanks
[quote=makecoldplayhistory ]I don't have a supervisor nor anyone to contact. There's no online discussion board.
I wasn't exaggerating when I said you are sent a reading list, examination dates and an assignment.
Sounds tough - the trouble is, everybody else on this thread is a coder, and it's hard for us to understand why it's difficult as we don't remember not being able to code! Though you have been given some links to places you can discuss stuff - best bet is usually to search for something similar (though not always easy to know what to search for - don't do it too much as you will irritate, but if you post a question which has been asked before usually somebody will link to a previous answer).
Does your reading list include a basic tutorial on coding? If not you could do worse than something like this: http://www.amazon.co.uk/Beginning-Programming-Java-Dummies-Computers/dp/1118407814/ref=dp_ob_title_bk
(as an experienced coder, when I want to learn something new I tend to google "NAME_OF_THING_I_WANT_TO LEARN tutorial", which comes up with http://docs.oracle.com/javase/tutorial/ for java, though this might be a bit tough for you - though you could try https://www.google.co.uk/#q=coding+tutorial)
I've got sketches (on several sheets of paper) from where I was working out the x,y co-ords manually.
How will they help me if it's being calculated inside Processing?
All you need to do is understand how you were working out the co-ords manually and turn that into pseudo code, which you can then turn into processing/java. Is one of the fundamental skills of coding.
Didn't realise this was still trundling along.
OP: you [i]REALLY[/i] need to get your head around basic flow-control (like for loops) and the ideas of assigning values to variable and calling functions.
[url= http://en.wikipedia.org/wiki/For_loop#Syntax ]The syntax varies[/url], but these core concepts are absolutely fundamental to programming in almost any language.
Here is a basic for-loop in Java/Processing:
[code]for (int i = 1; i < 4; i++)
{
my_function(i);
}[/code]
The three clauses between the brackets work as follows:
[code]int i = 1;[/code] - [i]the initialiser[/i]. This sets up the variables you will use to control the loop. It is only executed once. In this case it declares an integer named "i" and initialises it to 1. So in the first iteration of the loop i will be 1.
[code]i < 4;[/code] - [i]the condition[/i]. This is tested BEFORE each iteration of the loop. The loop is only executed when this is true. If it is false then the loop is finished and the program continues on to the code after the loop. Here it checks that i is less than 4.
[code]i++[/code] - [i]the afterthought[/i]. This is executed AFTER each iteration of the loop. It will usually modify the value of a variable that is tested in the [i]condition[/i] so that at some point the condition will be false. Here it just increments i by one.
So... that bit of code will work like this:
[list][li][i]Initialiser: [/i]Declare i and set it to 1[/li]
[li][i]Condition: [/i]Is i less than 4? Yes, okay carry on.[/li]
[li][i]Body: [/i][b]Call my_function(1)[/b][/li]
[li][i]Afterthought: [/i]Increment i. i is now 2.[/li]
[li][i]Condition: [/i]Is i less than 4? Yes, okay carry on.[/li]
[li][i]Body: [/i][b]Call my_function(2)[/b][/li]
[li][i]Afterthought: [/i]Increment i. i is now 3.[/li]
[li][i]Condition: [/i]Is i less than 4? Yes, okay carry on.[/li]
[li][i]Body: [/i][b]Call my_function(3)[/b][/li]
[li][i]Afterthought: [/i]Increment i. i is now 4.[/li]
[li][i]Condition: [/i]Is i less than 4? Nope, we're done.[/li]
[/list]
^^ Although convention is that you start with i = 0, so that the i < 4 limit will perform 4 iterations.
Indeed, zero is typical, but I thought it was important for the OP to realise it doesn't [i]have[/i] to be zero.
(Especially if he doesn't have to draw the case with zero circles 🙂 )
I've got sketches (on several sheets of paper) from where I was working out the x,y co-ords manually.
How will they help me if it's being calculated inside Processing?
One of the most fundamental skills in programming is working your way through your own thought processes and then turning that into a process you can write.
In this instance when you computed the positions for the circles by hand I'm imagining you went something like.
Ok, so I have to fit... lets say five... circles around the circumference of this circle so I guess that means they have to form 72 degree angles* with the centre of the middle circle and one another.Ok, that's easy so now I need to work out what the position is of the centre of the circles on the circumference of the circle where I'm starting - the first one's easy enough, it's straight up by the radius of the circle, but after that I need to use some trig. God I hate trig. Mr Wright at school had a stupid beard and smelt like chips, I could never really listen to him... Anyway, SOHCAHTOA and all that jazz.
ITERATION ONE (Let I = 1)
So the first one is going to be at 72 degrees to the first line, the radius of the circle will form the hypoteneuse of a triangle which will give me the coordinates to get from the middle of the circle to the centre of the new circle. Great. Erm... well the SOH part gives me opposite over hypoteneuse and the CAH part gives me the adjacent over the hypoteneuse so I'll need one of each. That means that the centre of the new circle will be at:
x = CentreMainCircle + RadiusMainCircle * sin(72)
y = CentreMainCircle + RadiusMainCircle * cos(72)So let's assume that the radius is 10 and the centre is at (0,0), does that look right - the point is at (9.5, 3.1) looks about right to me. Ok, draw my circle there. Boom!
ITERATION TWO (I = 2)
OK on a roll now, what's the new angle I need. Ok well it seems to be TWO times 72 degrees or 144 degrees. That gives mex = 0 + 10 * sin(144) = 5.9
y = 0 + 10 * cos(144) = -8.1Draw the circle at (5.9, -8.1). Looks good!
ITERATION THREE (I = 3)
Hmm... so now we're using THREE times 72 (Hmmm... I*72?) or 216 degrees.
x = 0 + 10 * sin(216) = -5.9
y = 0 + 10 * cos(216) = -8.1Hot damn I'm good.
ITERATION FOUR (I = 4)
So Let's go straight for it.
x = 0 + 10 * sin(I * (360/NumberOfCircles)) = -9.5
y = 0 + 10 * cos(I * (360/NumberOfCircles)) = -3.1Bosh, I AM INVINCIBLE!
Hmmm... If I do one more I reckon I wouldn't have needed to work out the initial circle's position first. sooooo...
ITERATION FIVE(I = NumberOfCircles )
x = 0 + 10 * sin(I * (360/NumberOfCircles)) = 0
y = 0 + 10 * cos(I * (360/NumberOfCircles)) = 10Amazing, job done!
So then, generalising it.
1.) Well I took a number of circles
2.) I divided 360 by the number of circles
3.) I looped from I = 1 up to I = the number of circles doing:
x = 0 + 10 * sin(I * (360/NumberOfCircles))
and
y = 0 + 10 * cos(I * (360/NumberOfCircles))to get pairs of coordinates
4.) I drew circles at each of those points.
5.) I danced around the room waving my hands in the air.*sooo...
var n = NumberOfCircles
var Ox = CentreOfMainCircleX
var Oy = CentreOfMainCircleY
var r = RadiusMainCircle
var r2 = RadiusLittleCircle
for I=1 to I=n do
var x = Ox + RadiusMainCircle * sin(I * (360/n))
var y = Oy + RadiusMainCircle * cos(I * (360/n))
elipse(x,y,r2,r2)
end for...
Profit
This might seem patronising but it's pretty much exactly how my brain will run through a problem like this and it serves me well enough to write code for a living. I'm sure I've made some trivial mistake in here but I hope it helps.
*6. Realise I should have done this in Radians
paste this into the editor. 0-50 circles adjusted with left and right arrow keys. It works.
int numberOfCircles = 10;
void setup() {
size (600, 600);
}
void draw() {
background (255, 255, 255);
ellipse(width/2, height/2, 200, 200);
float a = 360.0/numberOfCircles;
for (int i = 0; i < numberOfCircles; i++) {
float x = 210*cos((a*i) * PI / 180.0)+width/2;
float y = 210*sin((a*i) * PI / 180.0)+height/2;
ellipse(x, y, 20, 20);
}
}
void keyPressed() {
if (keyCode == LEFT) {
numberOfCircles = max(numberOfCircles - 1, 0);
}
if (keyCode == RIGHT) {
numberOfCircles = min(numberOfCircles + 1, 50);
}
}
Thanks for the replies.
I teach English as a 2nd Language for a living. I know how hard it can be to dumb things down enough; especially when they seem so simple to you.
Lemony - that certainly seems like a sensible approach. I'll try it with the next question!
With help from here, I got to
int nbr_circles = 8;
void setup() {
size(300,300);
smooth();
background(255);
}void draw() {
background(255);
float cx = width/2.0;
float cy = height/2.0;
fill(0);
//float x, y;
for (int i = 0; i < nbr_circles; i++)
{float angle = i * TWO_PI / nbr_circles;
float x = cx + 110.0 * cos(angle); // calculate x
float y = cy + 110.0 * sin(angle); // calculate y
ellipse(x, y, 20, 20);
}
}void mousePressed() {
if (mouseButton == LEFT) {
nbr_circles = nbr_circles + 1;
} else if (mouseButton == RIGHT) {
if (nbr_circles > 2) {
nbr_circles = nbr_circles - 1;
}
}
}
AND... understand what it all does.
It wasn't the idea of loops that I was struggling with, nor the formula, it was how it combines.
My brain, doesn't work like a coder but as a human (no offence). I mean, when I saw the problem, I thought I would calculate the 54 circles for n=2 to n = 8 and then get the code to display it.
Thanks agian for all help.
[quote=makecoldplayhistory ]I teach English as a 2nd Language for a living. I know how hard it can be to dumb things down enough; especially when they seem so simple to you.
You understand about how to learn then - I don't know why you're finding this so difficult 😉
With help from here, I got to[code]some code[/code]
AND... understand what it all does.
Yay!!!
You haven't got to the deadline yet have you? You can resubmit?
lol @lemonysam, that explains exactly what happens when doing maintenance stuff with another chap I work with who is far less geeky than me. It is frustrating that sometimes writing the script takes almost as long as it would have taken to do it by hand, but experience suggests that most things get repeated more than once, so that the actual trade off point ends up fairly close to where I first decided a script was needed.
I can't re-submit the code. I've submitted that particular question.
However, the next question says 'alter the code you have so that..."
I'll be able to use the code in the next section. Also, this isn't a coding module so much as perceptions of what's happening on the screen. The majority of the marks are for your academic writing about what you see the code doing.
We do need an accompanying running commentary on writing the code. I don't imagine I'll lose many marks for the code from section 1 being improved by section 2.
Other code, I didn't struggle with, was creating a square, offset in the window and rotating it about the centre of the window. You had to talk about how, as the frame rate increased, it became a circle on the screen. The code for this question was obviously a massive leap in complexity compared to
draw a square
make it rotate
right click mouse increases rotation speed
left click mouse decreases rotation speed
Thanks again for the replies.


