Home › Forums › Chat Forum › any coding bods around to help?
- This topic has 106 replies, 21 voices, and was last updated 9 years ago by GrahamS.
-
any coding bods around to help?
-
GrahamSFull Member
And this is for a Computer Science BSc??
Wow, things have changed a bit. When I did mine (20 odd years ago) I don’t think we even touched on graphics till the second year!
FlaperonFull MemberWhen I did mine 10 years ago we had a package called Turtle Java to introduce loops and things because it provided instant visual feedback into the concepts of loops / recursion / if-else etc.
“Computer Graphics” was an entirely separate module which I didn’t do.
CougarFull MemberIlama: I’ve never been taught anything about this by anyone.
Sorry, I’m going to pick up this because it sounds a bit… mental.
Every experience I’ve ever had of being taught something follows the pattern of “here’s a technique… now here’s an assignment for you to do using that technique to show you’ve understood it,” right back to when I was in high school. Indeed, you could be penalised for doing it “correctly” rather than for following what you’d been taught, as the implication would be that someone else did it for you. In the absence of other information, it’d make sense to me for lesson 1 to be “just do this manually and get it working” and lesson 2 to be “right, now we’re going to introduce loops and show you how much better they are” but it sounds like they’ve not even done that.
The other thing that strikes me is, for a “programming 101” situation, the task they’ve set is insanely difficult. For context, it’d give me pause to work out, and I was constructing FOR loops before I hit puberty. I’d argue that the maths side of it is much harder than the coding.
This is why pseudocode is so important, so you can work out your logic flow in natural language before you even think about getting code that parses. If you try to tackle it directly in code you’ll be spending all your time trying to understand why you’re getting variable type errors whilst simultaneously trying to rewrite the work flow so that it makes sense and that’s a challenging thing to do.
The manual method you’ve submitted is horrible code, as I’m sure you know, but I’m struggling to see what else they’d expect you to come up with when you’ve no previous experience and they’re not giving you any guidance.
CougarFull MemberFOR loops 101.
A FOR loop repeats code, so you don’t have to write the same things out over and over. Your manual approach may appear fine for five circles, but what if you’d to draw 20? A hundred?
The loop has an internal counter which you assign a variable to. You set parameters for the start and finish of the loop, and when you get to the end of the loop the counter is incremented by 1 automagically. Critically, this variable is available to you to frob about with inside your code, so you can use it to affect the contents of the loop or even just see where you’re going.
A basic (ho ho) example,
FOR f = 1 TO 10
PRINT f
NEXT f
Don’t worry about the syntax here (pseudocode, yes?), look at the concept. The first line says “hey, let’s start a loop. We’re using “f” as the counter variable, and we want to loop from 1 to 10.” The second line is your repeating code; here I’m just printing the contents of the variable on screen. The final line closes the loop, it’s the closing
}
in your actual code; the parser adds 1 to f and goes back to the start of the loop for another pass, unless f is 10 in which case it carts on with the rest of the code. Coding this up and running it, I’d expect to see something like,
1
2
3
4
5
6
7
8
9
10
(or perhaps
12345678910
, matters not)The froody bit about this is, in your example, you’ve got ‘f’ to tell you which circle you’re drawing. If you can programatically calculate the position of your circles individually using trig then you can do it in a loop.
Eg, lets take four circles (nice and easy to envisage), you’d have to find the positions at 0′, 90′, 180′ and 270′. I’m going to remove the maths for clarity – we’re breaking down the problem into bite-sized manageable chunks – and say that we have some handwavey maths which takes an angle and returns co-ordinates.
So, manually, you’d have
calculatePosition(0)
calculatePosition(90)
calculatePosition(180)
calculatePosition(270)
But, we can calculate those figures rather than hard-coding them.
calculatePosition(90*0)
calculatePosition(90*1)
calculatePosition(90*2)
calculatePosition(90*3)
See what’s happening here? We’re changing the value by an incrementing number, where have we seen this before? Let’s go loopy.
FOR f = 0 to 3
calculatePosition(90*f)
NEXT f
Simples, yes? The beauty of this is, apart from avoiding having to write the same code a squillion times, it’s portable. If we need a different number of circles, all that changes is that ’90’ figure. Where’s 90 come from? Of course, it’s a full circle divided by the number of circles we want. So what happens if we put a variable in there?
circles = 4
angle = 360/circles
FOR f = 0 to 3
calculatePosition(angle*f)
NEXT f
Boom, a chunk of pseudocode that will generate any number of circles from 1 to infinity.
If you’ve followed all this, you should now be thinking “that hard-coded ‘4’, how do I change that to generate successive images with 1, 2, 3, 4 circles? If only there was some form of code that repeated a code bloke using a number that went up by 1 each time…”
This is, fundamentally, the foundation to the solution for your assignment. The maths is a chunk of code that you’ll need to work out and it looks like you’ve got that largely nailed, it’s just a spot of trig. And at each stage you’ve got the task of turning pseudocode into real code of course.
The way you eat an elephant is one bite at a time, you cannot do the whole thing in one drop (or rather you might be able to, but you’re making life very difficult for yourself if you try). Break it down. Maths giving you gyp? Get it printing the numbers on the screen before worrying about what to do with those numbers. Loops a problem? Get the control structure down before you worry about what it’s doing inside that control. Or do it the other way, get the
calculatePosition
working first if that’s the area you’re more comfortable with, it’ll give your confidence a boost when you start to see parts of it working.One last thing, modern IDEs often give you the ability to trace through code but there’s nothing to stop you adding temporary code to punch up additional information whilst you debug it, eg, send your variable values to the screen if you want to see what’s going on.
CougarFull MemberOh, for completeness I should probably add,
FOR loops are typically incremented, but don’t have to be. Depending on your programming language you can usually tell it to decrement or go up in multiples if that’s what’s required.
I wouldn’t worry about that for now though, just figured it was worth throwing out there before someone corrects me. (-:
versesFull Member<pedantic_hat>
Shouldn’t;
circles = 4
angle = 360/circles
FOR f = 0 to 3
calculatePosition(90*f)
NEXT f
be;
circles = 4
angle = 360/circles
FOR f = 0 to 3
calculatePosition(angle*f)
NEXT f
aracerFree Membermcph, please ignore, I’m playing geek one upmanship with Cougar
or exponential:
for (i=1; i<256; i*=2)
or pretty much anything else you want to do:
for (i=1; i!=5; i=(i*2)%9)
…and you don’t have to test on the loop variable
for (i=0, input=0; input != ' '; i++) {
input = getchar()
}or even have a loop variable
for (input=0; input !=' '; input=getchar());
(though admittedly that would probably be better as a do…while loop)
I wouldn’t worry about that for now though, just figured it was worth throwing out there before someone corrects me. (-:
😉
CougarFull Member<pedantic_hat>
FFS, nice spot. I’ll abuse my privs and change it.
chambordFull Member@aracer or you can do this if you really want 🙂
for (;;) { printf("Look around you. "); }
CougarFull MemberI’m playing geek one upmanship with Cougar
Yeah, I think that’s beyond the scope of what I was trying to say (-: I was trying to simplify as much as possible, as the OP said he was struggling to get his head round it.
To be fair, I’ve very rusty when it comes to programming, I’ve never touched Java or even heard of “Procedure” or what ever it’s called, I’m assuming it’s some form of C derivative? But most of my coding these days is around system scripting and automation, login scripts and the like, and even then it’s not a common thing for me to have to do. There’s a reason my pseudocode looks a little unconventional…
MackemFull MemberMy 1st assignment in my computer science BSc was to write a drawing program that let you rotate the drawing. around a point. In Modula-2. This was in 1988. We did have programming lessons though.
aracerFree MemberProcessing, it’s Java. Java’s not a language I’ve ever done much with – writing apps for my phone is on my todo list, but not near the top – but it’s what I’d describe as a C style language, and at the level we’re working at here the code would also compile as C. It’s only when you get into the proper OO stuff it starts to differ from C++, and then there’s still not a huge difference.
whatnobeerFree MemberJava’s not a language I’ve ever done much with – writing apps for my phone is on my todo list, but not near the top
If your planning on using Android, yes it’s Java, but it’s a pretty different experience from working with Java in a more traditional setting. No more difficult, just different.
When I did mine 10 years ago we had a package called Turtle Java to introduce loops and things because it provided instant visual feedback into the concepts of loops / recursion / if-else etc.
“Computer Graphics” was an entirely separate module which I didn’t do.
I started my degree 7 years ago and that was the first thing we did too. Graphics was a 3rd or 4th year level module working with OpenGL. I did it and it was a lot of fun, but not one of the most popular courses in terms of class size.
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.
Stick at it. I had a long conversation at Christmas about how people who code are able to think about problems in a different way so that it becomes easy to solve the problem at hand. It doesn’t happen over night, but with practice you’ll get the hang of it.
monkeychildFree MemberIs the OP doing it by distance learning? If so I sympathise! I am not coding, but distance learning is bloody hard (currently on a path to a BSc in IT & Comms including ethical hacking).
makecoldplayhistoryFree MemberI’m on a coffee break during the next question so will try to reply.
Yes, it’s a distance learning degree. BSc through University of London, under-written by Goldsmiths.
This module is ‘Creative Computing, Image, Sound and Motion.’ The reading has had a little coding such as movement of a shape, rotation of a shape and drawing a shape. It’s mostly been about people’s perceptions of shape, how the mind is confused. Things like the impossible triangle, Rubin’s vase/face image etc.
From my reading, I’d understood FOR loops. I’d read and understood how to create a grid, a line etc and how the loops can be incremented (eg “i ++”).
It did seem like an enormous jump to get to this assignment. I’m glad you didn;t find it very simple, Cougar.
FWIW, I’m not impressed with the course, University. Whilst someone I know must join online discussion groups for each module, can email a module supervisor with questions etc, I get no help beyond the reading list. If I’m stuck, I’m stuck.
This isn’t so much a problem for maths (maths is maths is maths. Just read more!). History of computers (keep reading, you’ll remember it all).
Coding is another matter though. Little things like knowing you can right click for the reference. One of the hardest parts is knowing what you can and can’t do with the code. Thinking like a programmer (lemonysam’s graph).
I didn’t do any kind of IT at school past third year. My A levels were Economics, Physics, English and Graphics. The graphics was 100% set squares, compasses, pencils etc.
Time to crack on.
I’m now
“adapt the code so only one circle at a time is shown each in successive frames. What happens when the frame rate is increased, decreased. How about when the number of circles is changed?”
The frustrating part is by far the majority of the marks come from your observations, not the code.
Thinking like a programmer, and following advice from this thread, I’m writing out what the code needs to do.
Do I need to use another loop? I think I can somehow use the i value to draw a particular circle. Somehow nesting another loop so, if i = 1 draw circle 1, if i = 2, draw circle 2 etc.
Or, and I need to get back to reading, can i say, draw result 1 (ellipse) in frame 1, then draw result 2 in frame 2.
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);
float y = cy + 110.0 * sin(angle);
ellipse(x, y, 20, 20);
}
}whatnobeerFree MemberFrom looking at the code i knocked together previously you only need to change a few lines.
(I hope you’ve got this working already, but if not, read on)Or, and I need to get back to reading, can i say, draw result 1 (ellipse) in frame 1, then draw result 2 in frame 2.
This is the right line of thought. Remember that the draw() method is called each frame. Think about what you need to do and what you need to change to only draw 1 circle per frame and what needs to be incremented each time draw is called to do that. Also remember that most, if not all the methods you need are on the Processing website[/url] You can read about how the draw loop works and find methods to control the frame rate, clear the buffer etc there.
makecoldplayhistoryFree MemberI’m not close to getting it working (but am close to tears).
I got to this, but couldn’t get it to work
int nbr_circles = 1;
void setup() {
size(600, 600);
smooth();
background(255);
}void draw() {
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);
float y = cy + 110.0 * sin(angle);ellipse(x, y, 20, 20);
if () {
angle = (0.68)
float c = angle
translate (width/2.0, height/2.0)
rotate (c);
ellipse(x,y,20,20)}
My latest (still not working) code is
int nbr_circles = 1;
void setup() {
size(600, 600);
smooth();
background(255);
}void draw() {
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);
float y = cy + 110.0 * sin(angle);
ellipse(x, y, 20, 20);
}
for (int i = 0; i = nbr_circles; i++)
{
translate (width/2.0, height/2.0);
rotate (radians());
}
}The trouble is, when I can’t get it working, there are so many reasons it may not be, I don’t know where to begin to sort it out.
GrahamSFull MemberWhy are you translating and rotating?
I don’t think it asked you to do that. It just wants you display one circle at a time in quick succession.So you ditch the for-loop and instead increment which circle is displayed yourself on every frame.
makecoldplayhistoryFree MemberCan you look at this whatnobeer (or anyone else who’ll help)?
whatnobeerFree MemberEarlier you had code that could draw an arbitrary number of equally spaced circles. What you need now is to draw one circle at a time each frame. It’s like you said, draw circle number 1, then draw circle number 2…. Etc. Remember, the draw() method is essentially an infinite loop, where each loop is a frame, so use that loop instead of another for loop. You might want to look at the example for the draw method to get inspiration on how to track how many times the loop has been called.
makecoldplayhistoryFree MemberGraham – It didn’t ask for that but I’ve spent 9 1/2 hours today trying to “increment which circle is displayed”
I need to keep the for loop, don’t I, so that x,y can be calculated if n changes.
I started off the day trying to use if / else,
based on the code at the bottom of the page, something like,
if i increases by 1, draw the new ellipse
I could never get it to work. Things like it saying, no value for i. When I gave I a value [int i = 0;] then it broke the code somehow.
I can’t actually remember all of the things I tried which failed. I’ve been sat at my computer since Friday morning!
int nbr_circles = 10;
void setup() {
size(600, 600);
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);
float y = cy + 110.0 * sin(angle);ellipse(x, y, 20, 20);
}
GlennQuagmireFree MemberYou need to start the loop with i = 1 otherwise it will fail on the first iteration
so: for (int i = 1; i < nbr_circles; i++)
i is just a count so it deosn’t really matter what value it starts with.
GrahamSFull Member> It just wants you display one circle at a time in quick succession.
Graham – It didn’t ask for that
I’m pretty sure that’s what they meant by “adapt the code so only one circle at a time is shown each in successive frame”
So if you currently have it set to draw 4 circles then on frame 0 you draw circle 0, on frame 1 you draw circle 1, on the frame 2 you draw circle 2, on the frame 3 you draw circle 3 and on the frame 4 you go back to drawing circle 0.
I need to keep the for loop, don’t I, so that x,y can be calculated if n changes.
No, you need the body of that loop to calculate x and y for the circle you are currently drawing, but you no longer need the loop itself because you no longer need to draw all the circles in one go.
So…
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);
}Then let’s replace “i” with something a bit more descriptive like “currentCircle”
And each time through the draw() routine you need to increase that by 1:
currentCircle++;
But you don’t want it to be greater than the number of circles so you check that after you increment it:
if (currentCircle >= nbr_circles)
currentCircle = 0;(Or if you want to be fancy pants you could do all that in one go using a modulus operator:
currentCircle = (currentCircle + 1) % nbr_circles;
but don’t worry if that is too confusing, just use the first version)You need to start the loop with i = 1 otherwise it will fail on the first iteration
so: for (int i = 1; i < nbr_circles; i++)
Umm, no it won’t? And your suggestion would give you one less circle than you need. 😀
aracerFree MemberI’ll try and make it a bit simpler for you.
Yes and no. Yes you still need a loop, but no you don’t need the for loop. Sorry that sounds weird, but I’ll try and explain…
There’s nothing particularly special about a for loop, it just increments a variable and calls a set of functions with that variable, then checks whether the variable has reached a limit. You don’t need a for loop for that, you can do it yourself.
For this new exercise you have to to it yourself. In the same way standard java automatically iterates a for loop, processing automatically iterates the draw() function, so you can use that as a loop. Just do the three things a for loop does:
A) initialise the loop variable
B) increment the loop variable
C) check the loop variable against the limitB and C need to happen each iteration of the loop, so should be inside the draw() function. A should only happen once, so needs to be outside it.
GrahamSFull MemberThere is a second thread now aracer – though he still seems to be a bit stuck.
http://singletrackworld.com/forum/topic/coding-bods-part-ii-if-you-still-have-patience
The topic ‘any coding bods around to help?’ is closed to new replies.