Home Forums Chat Forum any coding bods around to help?

Viewing 40 posts - 1 through 40 (of 107 total)
  • any coding bods around to help?
  • makecoldplayhistory
    Free Member

    very stuck in Java / Processing.

    whatnobeer
    Free Member

    What’s the problem?

    makecoldplayhistory
    Free Member

    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 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 ();

    //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!

    GrahamS
    Full Member

    I think 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

    molgrips
    Free Member

    Hmm.. I think I would not be happy with hard coding the coordinates like that…

    molgrips
    Free Member

    Presumably if you can work out on paper you can work it out in Java?

    makecoldplayhistory
    Free Member

    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!

    funkynick
    Full Member

    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… Oracle have some tutorials on their site

    Mackem
    Full Member

    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
    Free Member

    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.

    GrahamS
    Full Member

    Okay, so your formulae are the Parametric Equation for a Circle (plus an offset). That’s fine. Click here to visualise it.

    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.

    makecoldplayhistory
    Free Member

    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

    GrahamS
    Full Member

    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:

    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

    toby1
    Full Member

    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 Stackoverflow[/url] so the answers you are searching for will no doubt already be out there somewhere.

    makecoldplayhistory
    Free Member

    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

    GrahamS
    Full Member

    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.

    llama
    Full Member

    what is the subject you are studying?

    aracer
    Free Member

    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?

    GrahamS
    Full Member

    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:

    int value, b, c1, c2, c3, c4, c5, c6;

    You are declaring that these variables will all hold integer values (i.e. whole numbers).

    When you say:

    c1 = ellipse (300, 190, 20, 20);

    You are NOT saying “set c1 to this ellipse function”

    You are saying “call the ellipse function right now and set c1 to be the integer that the function returns when it is finished”

    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”)

    Flaperon
    Full Member

    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?

    makecoldplayhistory
    Free Member

    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

    aracer
    Free Member

    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.

    chambord
    Full Member

    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.

    makecoldplayhistory
    Free Member

    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.

    toby1
    Full Member

    Sorry I wasn’t suggesting posting the question on stackoverflow, more using the existing information on there to help answer the question.

    allthegear
    Free Member

    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

    Mackem
    Full Member

    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

    aracer
    Free Member

    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.

    chambord
    Full Member

    we used a language called Logo

    *Remembers many days spent using that and sheds a small tear*

    aracer
    Free Member

    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).

    llama
    Full Member

    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.

    Flaperon
    Full Member

    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).

    allthepies
    Free Member

    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.

    aracer
    Free Member

    He’s already got that, as it’s one of the add ons he gets with processing.

    mogrim
    Full Member

    A java “for” loop:


    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);
    }

    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…

    GrahamS
    Full Member

    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.

    allthegear
    Free Member

    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

    molgrips
    Free Member

    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:

    http://processing.org/

    Appears to be not at all object orientated…

    oldnpastit
    Full Member

    Appears to be not at all object orientated…

    It’s all about template metaprogramming and functional languages these days. OO is soooooo 2009.

    GrahamS
    Full Member

    Let’s not freak out the OP toooo much eh? 😀

Viewing 40 posts - 1 through 40 (of 107 total)

The topic ‘any coding bods around to help?’ is closed to new replies.