I want to use a for loop to move a drawing between two points. However the loop is too fast to see all the positions. So I am trying to insert a pause in the loop but it doesn't work. Is there any solution compatible with a for loop ?
Thank you in advance.
/*
Circle moving between two points chosen among a set of random ones
I can't succeed in extracting the centroid from a RShape.
I proceed this way:
1. I load the shape from an SVG file:
RShape myShape = RG.loadShape("test3-2.svg");
2. I use this instruction, which seems mandatory (althought I don't really understand what it does) :
myShape = RG.centerIn(myShape, g);
3. I draw the shape:
RG.shape(myShape);
4. I use the getCentroid method like this:
RPoint myShapeCent= myShape.getCentroid();
There is no error message caused by the former instruction but if I try to use the RPoint's data (myShapeCent.x for instance) I get a null pointer error message.
I have problems with very basic things which are neither clearly documented in the reference nor illustrated with specific examples.
I have an SVG file including several paths. I want to put each path at a different place on the screen. Therefore I try to use pushMatrix() and popMatrix() around scale(), translate() and shape(RShape). However these push/pop instructions seem to be ignored. Therefore each path's position depends on position of the previous path which is very annoying. So I try to use geomerative's own translate and scale methods instead but it doesn't succeed : neither scaling nor translation is applied.
What's wrong ???????
import processing.xml.*;
import processing.opengl.*;
import geomerative.*;
//shapes
RShape globalShape;
RShape antwerp;
RShape brussels;
RShape oostVlaanderen;
RShape westVlaanderen;
RShape hainaut;
RShape liege;
RShape brabantWallon;
RShape vlaamsBrabant;
RShape limburg;
RShape eupenMalmedy;
RShape namur;
RShape luxembourg;
//positions for translation of children shapes (copied from SVG file's "M" field)
I have a weird problem. I am using RShape objects from geomerative library to display different children shapes coming from one global shape because I want to fill each child with a different color evolving in time. When the .SVG file is displayed in one piece, all the children shapes insert in each other correctly. However if I display each child separately, the relative sizes of all the children are not correct. I must downscale some shapes and upscale others and it's hard to find the correct resizing factor for each shape. I must translate each shape one after another too so that it finds its relative place again.
So, is there a way to find the correct scaling factor for each child (and maybe the position on plane) within the SVG data ? I guess these data must exist in the SVG file, otherwise how could the global picture be drawn correctly ? Or is there another trick to avoid such a painful puzzle ? Can I solve this within Processing easily or would it be better to modify something in the SVG file using Inkscape ?
I want to test if random points are inside this polygon or not. I am using the java.awt.Polygon class
as described on the wiki.
However the
contains method returns true only if a point is exactly on the border rather than if it's simply inside the polygon. Should I use another method, algorithm or could it come from an error in the polygon definition ? Maybe it has too many points ? (replacing the drawing trough the Polygon class by beginShape()-endShape() sequence leaves the polygon unfilled too).
Thank you in advance.
/**
program to try inclusion test (java.awt.Polygon) of random points in complex polygon
I want to apply the applet signing tutorial from the Wiki.
1. When exporting, is it normal that I get several jar files (one for the sketch, one for the core library, one for opengl) even if the corresponding option within the preferences is not checked ?
2. Can I use the same keystore file for all my jar files ?
3. Should I copy the keystore file with the other data on the website ?
4. My first attempts with signature caused this message to appear: "The signer certificate will expire within six months.". Does it imply the applet won't be authorized anymore after six months, or does it only mean that I'll have to create a new certificate to sign new applets in six months ?
I can't figure out how to extract the data from the points created from an SVG loaded within an RG object.
The points seem to be created correctly (the
println(points.length); line returns a positive number, not a null). However when calling the mousePressed function, I receive a
NullPointerException message.
Actually this could be a syntax error too, I am not sure how to deal with arrays of objects...
Is there a tutorial or guidelines about insertion of pure Java code in Processing sketches somewhere ? Currently, I'd see that insertion as a way to use additional classes/methods like those
described here, which seem quite useful to handle arrays. They belong to this huge
VisAD thing.
So another tutorial, about addition (and compilation ?) of externals Java libraries within Processing, would be welcome too.
I know there are some unsolved issues with ortho() (
http://code.google.com/p/processing/issues/detail?id=37). However, using OpenGL as renderer, is it normal that I get a "
ortho(), or this particular variation of it, is not available with this renderer." message ? Does it has anything to do with the current bugs or am I missing something ?
Otherwise would it be possible to have an orthographic-equivalent projection using perspective() with specific settings ?
In the sketch below, a 2D grid is drawn and inserted in a 3D space. I want to be able to move some grid's points. Using the screenX and screenY functions I know when my mouse pointer is over a point. However as there is no reciprocal ("screen to world") function in Processing, I am using some code found on the older forum. It seems that it relies on Java 2D so I get this incompatibility message :
ClassCastException: processing.core.PGraphicsJava2D cannot be cast to processing.opengl.PGraphicsOpenGL
The message is caused by the code at lines 40 and 41:
gl=((PGraphicsOpenGL)g).gl; // for screen to world glu=((PGraphicsOpenGL)g).glu; // same here
So, is there a "pure" OpenGL replacement for this?
Thank you in advance.
//
// Grid creation with rows x cols points. Plane is set in an OpenGL 3D scene. Intersections can be moved.
//
import controlP5.*;
import processing.opengl.*;
import codeanticode.glgraphics.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import java.nio.*;
ControlP5 controlP5;
GL gl; // for mouse to world conversion
GLU glu; // same here
int rows = 15; // number of rows
int cols = 8; // number of cols
int points = rows * cols ; // number of points
int x_size = 500; // width
int y_size = 500; // height
int margin = 50; // margin
int button_height = 100; // additional height for button zone
//int coords = 2; // number of coordinates per point (2D at the moment)
int bgColor = color(255, 255, 255); // background color
int squareColor = color(128, 0, 0); // square color upon mouse over point
boolean bover = false;
boolean locked = false;
int movingPoint = -1 ; // number of selected point
PVector[] mesPoints = new PVector[points];
// this is only a note.
// we will not use variable b in the code below.
// we have to use controlP5.Button here since there
// would be a conflict if we only use Button to declare button b.
controlP5.Button b;
// a button-controller with name buttonValue will change the
// value of this variable when pressed.
//int buttonValue = 0;
//********** SETUP
void setup()
{
gl=((PGraphicsOpenGL)g).gl; // for screen to world
//************* SCREEN TO WORLD FUNCTION - found on old Processing forum ********************
float[] getMouse3D()
{
((PGraphicsOpenGL)g).beginGL();
int viewport[] = new int[4];
double[] proj=new double[16];
double[] model=new double[16];
gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
gl.glGetDoublev(GL.GL_PROJECTION_MATRIX,proj,0);
gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX,model,0);
// java.nio.FloatBuffer fb = java.nio.ByteBuffer.allocateDirect(4).order(java.nio.ByteOrder.nativeOrder()).asFloatBuffer(); // replaces line below if java.nio not available
This sketch draws a 2D grid and allows to drag points. I'd like to achieve the same results in 3D.
Which classes/library would you recommend ?
Also, how can I insert a different number per line within the code in my messages to the forum ?
Thank you in advance.
Nesnduma
// // Mesh creation m x n points with mobile vertices //
import controlP5.*;
ControlP5 controlP5;
int rows = 15; // number of rows int cols = 8; // number of cols int points = rows * cols ; // number of points int x_size = 500; // width int y_size = 500; // height int margin = 50; // margin int button_height = 100; // additional height for button zone //int coords = 2; // number of coordinates per point (2D at the moment) int bgColor = color(255, 255, 255); // background color int squareColor = color(128, 0, 0); // square color upon mouse over point boolean bover = false; boolean locked = false; int movingPoint = -1 ; // number of selected point PVector[] mesPoints = new PVector[points];
// this is only a note. // we will not use variable b in the code below. // we have to use controlP5.Button here since there // would be a conflict if we only use Button to declare button b. controlP5.Button b;
// a button-controller with name buttonValue will change the // value of this variable when pressed. //int buttonValue = 0;
//********** SETUP void setup() { smooth(); ellipseMode(CENTER); rectMode(CENTER); size(x_size, (y_size+button_height)); background(bgColor); //Button controlP5 = new ControlP5(this); controlP5.addButton("Bouton1",0,50, (y_size+50),88,21); controlP5.setColorActive(color(255, 0, 0)); // color for mouse-over controlP5.setColorBackground(color(128, 0, 0)); // default background color controlP5.setColorLabel(color(255, 255, 255)); // text color controlP5.controller("Bouton1").setLabel("Regenerate points"); // button label //controlP5.setColorForeground(color(128, 0, 0)); // default color // Creates positions for points for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { int current_point = (cols * i)+j; mesPoints[current_point] = new PVector( ((((x_size - (2*margin))/(cols-1)) * j)+margin), ((((y_size - (2*margin))/(rows-1)) * i)+margin)); } } }
//********** DRAW void draw() { // Erases everything - redraws lines background(bgColor); // Draw lines strokeWeight(2); for (int i = 0; i < rows ; i++) { for (int j = 0; j < cols ; j++) { int current_point = (cols * i)+j; //Draw horizontal lines if (j < cols - 1) { // all but last column if ((i == 0) || (i == (rows - 1))) { // first and last rows stroke(0, 0, 0); // outer lines are black } else { stroke(0,192,255); // inner lines are blue } line(mesPoints[current_point].x, mesPoints[current_point].y, mesPoints[current_point+1].x, mesPoints[current_point+1].y); } //Draw vertical lines if (i < rows - 1) { // all but last line if ((j == 0) || (j == (cols - 1))) { // first and last column stroke(0, 0, 0); // outer lines are black } else { stroke(0,192,255); // inner lines are blue } line(mesPoints[current_point].x, mesPoints[current_point].y, mesPoints[current_point+cols].x, mesPoints[current_point+cols].y); } } }
// Draws points strokeWeight(1); for (int i = 0; i < (rows * cols) ; i++) { if ( (i % cols == 0) || (i % cols == (cols - 1)) || (((i+1) / cols) == 0) || (((i+1) / cols) == rows - 1 ) ) { //outer points stroke(0,0, 0); fill(0,0,0); } else { // inner points stroke(0,192, 255); fill(255,255,0); } ellipse(mesPoints[i].x, mesPoints[i].y, 10, 10); }
//Tests whether mouse rolls over points and draws red squares strokeWeight(1); if (locked) { squareColor = color(255, 0, 0); } else { squareColor = color(128, 0, 0); } stroke(squareColor); fill(squareColor); for (int i = 0; i < points; i++) { if(dist(mouseX, mouseY, mesPoints[i].x, mesPoints[i].y) <= 5) { movingPoint = i; rect(mesPoints[i].x, mesPoints[i].y, 10, 10); i = points ; //not necessary to go on with loop } else { movingPoint = -1; } } if (movingPoint != -1) { bover = true ; } else { bover = false; } // }
This page provides Java classes to receive Magic Trackpad's data within a Java program under MacOS X. Would it require much work to adapt it to Processing (maybe as a library) ?
I am running Processing 1.2.1 with toxiclibs-complete-0020 library.
I think there is a problem with the library import. When I try to run the following example (included with the library) I receive a 'Cannot find a class or type named "Triangle" ' message.
What's wrong ?
Also, it is normal that it is written "include toxi.geom..." rather than "include toxi.toxiclibscore.geom...", as the geom classes are included in the toxiclibscore package ?
Thank you in advance.
/** * <p>Manual mesh face extrusion</p> * * <p><strong>Usage:</strong> * <ul> * <li>f - toggle filled/wireframe display</li> * <li>a/d - move extruded face along X axis</li> * <li>w/s - moved face along Y</li> * <li>z/x - move face along Z</li> * </ul></p> */
/* * Copyright (c) 2010 Karsten Schmidt * * This demo & library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * http://creativecommons.org/licenses/LGPL/2.1/ * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
import toxi.processing.*;
import toxi.geom.*; import toxi.geom.mesh.*;
TriangleMesh mesh; boolean isFilled = true;
ToxiclibsSupport gfx;
void setup() { size(680, 382, P3D); gfx = new ToxiclibsSupport(this); // create a mesh from the axis-aligned bounding box (AABB) mesh = (TriangleMesh)new AABB(new Vec3D(), 100).toMesh(); // get first face/triangle of mesh Face f = mesh.faces.get(0); // extrude along positive Z axis and shrink to 25% of original size // shrinking is done by moving vertices towards centroid of the face // before extruding float shrink=0.25; Vec3D centroid = new Triangle(f.a, f.b, f.c).computeCentroid(); Vec3D extrude = new Vec3D(0, 0, 100); Vec3D a = f.a.interpolateTo(centroid, 1-shrink).add(extrude); Vec3D b = f.b.interpolateTo(centroid, 1-shrink).add(extrude); Vec3D c = f.c.interpolateTo(centroid, 1-shrink).add(extrude); // begin by adding new side faces: // side A mesh.addFace(f.a, a, f.c); mesh.addFace(a, c, f.c); // side B mesh.addFace(f.a, b, a); mesh.addFace(f.a, f.b, b); // side C mesh.addFace(f.c, c, f.b); mesh.addFace(c, b, f.b); // remove original face mesh.faces.remove(0); // add new face as cap mesh.addFace(a, b, c); // update normals (for shading) mesh.computeVertexNormals(); }
When the mouse is dragged close to a vertex, it is moved with the pointer. However there are two problems:
1. If I move the mouse too fast, the vertex is "lost" by the mouse and doesn't move anymore.
2. If a vertex is too close to another, they both move with the mouse.
Any idea ?
Thank you in advance.
Nesnduma
// // Displacement of vertices by mousedrag // int points = 4; int coords = 2; int bgColor = color(255, 255, 255);
int[][] mesPoints = new int[points][coords];
//********** SETUP void setup() { smooth(); ellipseMode(CENTER); rectMode(CENTER); size(400, 400); background(bgColor); // Creates random positions for points // Two embedded loops for (int i = 0; i < points; i++) { for (int j = 0; j < coords; j++) { mesPoints[i][j] = int(random(0,400)); } print("Point "); println(i+" :"); println(mesPoints[i]); } }
//********** DRAW void draw() { // Erases everything - redraws lines background(bgColor); for (int i = 0; i < points ; i++) { // Draws lines strokeWeight(2); stroke(0,192,255); line(mesPoints[i][0], mesPoints[i][1], mesPoints[(i+1)%points][0], mesPoints[(i+1)%points][1]); } // Draws points for (int i = 0; i < points ; i++) { strokeWeight(1); // Points stroke(0,192, 255); fill(255,255,0); ellipse(float(mesPoints[i][0]), float(mesPoints[i][1]), 10., 10.); }
//Test whether mouse rolls over points and draws red squares strokeWeight(1); stroke(255,0,0); fill(255,0,0); for (int i = 0; i < points; i++) { if((abs(mouseX - mesPoints[i][0]) <= 5) && (abs(mouseY - mesPoints[i][1]) <= 5)) { rect(mesPoints[i][0], mesPoints[i][1], 10, 10); } } }