gametheory — 2009-02-05 14:26:31

i'm completely new to the programming world and i'm very frustrated at my inability to write code in java and processing......yet i do not lose hill though the uphill battle makes me bananas.....whatever....i need to write a processing program whereby i use mouse control to draw a point on the drawing window when the mouse button is pressed.....so far so good i did manage that bit....however i am then bugged down by the idea of extending this program to place a succession of points each connected to the next by a straight line segment so that when a key on the keyboard is pressed the sketch is terminated by drawing a line from the last point to the first. i though of using arrays and a for loop but as yet havent come to solving this and writing the code.......and then i need to store the coordinate pairs in a file......

everybody tells me to practise....and rightly so..but is there anyone who can help me at least indicating useful tutotial sites abt processing and java please...

many thnks

regards

gametheory

22_80 — 2009-02-05 18:54:41

Hi,

Take a look at http://codelab.fr/39

+
+

emoc — 2009-02-05 23:57:54

Hi Gametheory,

You need a two-dimensional array to keep point coordinates, then for each mouse click add mouseX and mouseY of the click (using mousePressed) as a new point coordinates. In the draw() part of your sketch, redraw this array each frame with point() and line().

I could give you code but you want to learn by yourself, don't you ? :)

If you get stuck or lose patience, just ask...

gametheory — 2009-02-06 00:39:17

thanks a million i will work it out from there

i appreciate
regards

gametheory — 2009-02-10 19:28:16

further to my problem whereby i had to plot points on d screen and join them by a straight line segment which stops once a key is pressed that is d drawing ends this is what i managed to come up with:

boolean drawVertex = false;

void setup(){
 
  size(450,450);
 
  background(25,88,126);
  strokeWeight(30);
 
int [] x = {50, 67, 89, 124, 345};

int [] y = { 98, 56,45,125, 56};

beginShape();

for(int i = 0; i < x.length; i++){
 
  point (x[i], y[i]);
}

beginShape();

for(int i = 0; i < x.length; i++){
 
  vertex (x[i], y[i]);
}


  void keyPressed() {
 
  if ((key == 'A')){
    endShape(CLOSE) = true;
}

}
}

whereby im missing the stage whereby the vertex actaully passes from one point to another..........among the errors is dat d compiler points out that void keyPressed is an unexpected token.....

thnks

ps i just tried this too...............
boolean drawVertex = false;

void setup(){
 
  size(500,500);
 
  strokeWeight (18);
 
}

void draw(){
 
  background(45,200,88);
 
  if (drawVertex == true) {
    beginShape ();
    point (25,450);
    point (45,156);
    endShape();
  }
}

void keyPressed() {
 
  if ((key == 'A')) {
   
    drawVertex = true;
  }
}

void keyReleased(){
 
  drawVertex = false;
 
}
but i need to see the line being plotted.........maybe frameRate() ??

thnks

emoc — 2009-02-10 22:40:33

Your first attempt would work with some minor changes : add draw(), and put keyPressed() out from setup() and draw() as shown below, pressing caps A closes the shape.

Code (p5) :

boolean drawVertex = false;
int [] x = {50, 67, 89, 124, 345};
int [] y = { 98, 56,45,125, 56};

void setup(){
  size(450,450);
  background(25,88,126);
  strokeWeight(30);
}

void draw() {
  beginShape();
  for(int i = 0; i < x.length; i++){
    point (x[i], y[i]);
    vertex (x[i], y[i]);
  }
}

void keyPressed() {
  if ((key == 'A')){
    endShape(CLOSE);
  }
}
gametheory — 2009-02-10 22:54:33

thanks a lot but i need to see dpoints plotting from one point to another as in slow motion......maybe i should use threading???

thanks again

22_80 — 2009-02-11 13:03:51

Try
frameRate(1); in your setup() method : http://www.processing.org/reference/frameRate_.html

+
+

gametheory — 2009-02-17 13:03:40

hi itsme again guys.....i have manged to write a processing program whereby i load a text file called positions and plot the corresponding points. However in order to draw these points and calling the draw method, the program doesn't respond....this is my code so far....any help gretaly appreciated

thanks



PrintWriter output;

void setup()
{
   
   size(1024,768);
   
   strokeWeight(7);
   
   output = createWriter("positions.txt");
}
   
   void draw()
   {
     
     if (mousePressed)
     {
       
       output.println(mouseX + "\t" + mouseY + "\n");
       point(20,120);
       point(5,180);
       point(16,200);
       point(100,60);
       point(120,40);
       point(70,20);
       
     }
     
     if (keyPressed)
     {output.flush();
     output.close();
     }
   }

that is as regards the plotting of points.....if i need to use draw these points should i use a for loop in the draw method?

plus how will draw identify these points? shouldi store them in an array 2 dimensional array first?

thnks

emoc — 2009-02-19 10:52:18

Hi Gametheory,

citation :

if i need to use draw these points should i use a for loop in the draw method?
plus how will draw identify these points? shouldi store them in an array 2 dimensional array first?

Yes, read the file contents into an array, then from the draw method, call a function to draw your array from point 0 to point X, point X should be 1 at the first step, 2 at next step, etc. You could use millis() to define this step.

Here are some bits of code that could be useful :

Code (p5) :

int maxpoints = 1000;
int nb = 0;
float points[][] = new float[maxpoints][2];

void setup() {
  size(400, 400); 
}

void draw() {
  background(255,255,255);  
  stroke(0);
  frameRate(25);
  drawPoints();
}

void mousePressed() {
  nb ++;
  points[nb][0] = mouseX;
  points[nb][1] = mouseY;
}

void drawPoints() {
  if (nb > 0) {
    for (int i = 0; i <= nb; i++) {
      point(points[i][0], points[i][1]);
    }
    if (nb > 1) {
      for (int i = 2; i <= nb; i++) {
        line(points[i-1][0], points[i-1][1], points[i][0], points[i][1]);
        if (i == nb) {
            line(points[1][0], points[1][1], points[i][0], points[i][1]);
        }
      }
    }
  }
}
gametheory — 2009-02-19 10:54:48

many thanks....i will look in it

regards

gametheory — 2009-02-23 11:02:15

just tried it out.....many thanks....!!! i appreciate

regards

gametheory — 2009-03-01 10:01:47

its me again.....i must design a 3d teapot....and i would like to design curves....would it be useful to use curveVertex or curveBezier??

thnks

gametheory — 2009-03-01 17:01:47

if i use vertex for a 3d structure must i use three parameters one of which would be negative??

thnks

emoc — 2009-03-02 22:12:26

gametheory a écrit:

its me again.....i must design a 3d teapot....and i would like to design curves....would it be useful to use curveVertex or curveBezier??

bezierVertex will give you more control over the curves drawn, with control points, but it's harder to use. curveVertex only need points coordinates and the curves will be drawn passing by these points.

gametheory a écrit:

if i use vertex for a 3d structure must i use three parameters one of which would be negative??

You'll need points within a 3d space, with 3 coordinates, one for each axis : x, y ,z . Those coordinates could be positive or negative. You should take a look at processing 3D examples : http://processing.org/learning/3d/

gametheory — 2009-03-03 13:50:09

many thanks....

gametheory — 2009-03-04 07:58:19

i am in the process of writing code in order to design a teapot....as yet after consulting some other codes and modifying it i only managed to come up with the result of a kind of a cylinder. My problem is actually how to manage to draw the curve which shoots off from the base in such a way that it meets with a line at the top and then at the end of this line or surface i design the same curved side back again. Sphere does not help here because the sides of a teapot must bulge out....

To start off do i need to calculate an angle between the line that is the base and the curvature....use of sin() and cos() and a for loop??? As in i supply the bezier points and then make a for loop...in order to multiply the number of curvatures on a 3d base....???

a further problem arises with the spout....not the actual construction as such but where in the code should i write the commands for the drawing of the spout??

up till now i just came up with this code....void setup(){
  size(900,900,P3D);
 
}

void draw(){
  background(0);
  lights();
  translate(width/2, height/2);
  rotateY(map(mouseX, 9,width,9,PI));
  rotateZ(map(mouseY, 9,height,9,PI));
  noStroke();
  fill(255,255,255);
  translate(0,-40,0);
  drawCylinder(190,180,200,16);
 
}

void drawCylinder(float topRadius, float bottomRadius, float tall,int sides){
  float angle = 0;
  float angleIncrement = TWO_PI / sides;
  beginShape(QUAD_STRIP);
  for(int i = 0; i < sides + 4; i++) {
   
    vertex(topRadius*cos(angle), 0, topRadius*sin(angle));
    vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));
    angle += angleIncrement;
  }
 
  endShape();
 
  if(topRadius != 0) {
    angle = 200;
    beginShape(TRIANGLE_FAN);
    vertex(29,29,29);
    for(int i = 0; i < sides + 1; i++) {
      vertex(topRadius * cos(angle), 0, topRadius * sin(angle));
      angle =+ angleIncrement;
    }
   
    endShape();
  }
 
  if(bottomRadius != 40) {
    angle = 80;
    beginShape(TRIANGLE_FAN);
    vertex(0, tall, 0);
    for(int i = 0; i < sides+88; i++) {
      vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle));
      angle += angleIncrement;
     
    }
   
    endShape();
  }
}

it seems that one use of shape building is the use of QUADS_SRIPS do you think this will enable me to design the teapot??

kind regards and THANKS

cheers