I want to make a image move with sin graph, in processing - processing

PImage img;
float x;
void setup() {
img = loadImage("img.png");
}
void draw() {
}
How can I accomplish this?

When you get started I recommend carefully reading the available documentation , tutorial. You can also find an exhaustive answer on using the sine function in general and applying to movement here
Additionally, have a look at the Stackoverflow Tour to get an idea of how the community works (and earn a badge doing so).

Related

i need help on my assignment with moving an object using parameters in processing

I'm new to coding and I am trying to write a program that shows a car moving. I can't figure out how to move the car back and forth using parameters. Any help would be appreciated.
void setup()
{
size(500,500);
rectMode(CORNER);
ellipseMode(CORNER);
}
void draw()
{
background(50,200,255);
drawCar(80,340);
drawWheel(45,410);
}
void drawCar(int x, int y)
{
noStroke();
fill(255,0,0);
beginShape();
vertex(x,y);
vertex(x+50,y);
vertex(x+80,y+50);
vertex(x+110,y+50);
vertex(x+110,y+80);
vertex(x-60,y+80);
vertex(x-60,y+50);
vertex(x-30,y+50);
endShape(CLOSE);
}
void drawWheel(int wx,int wy)
{
fill(0);
noStroke();
ellipse(wx,wy,40,40);
ellipse(wx+85,wy,40,40);
}
It's hard to help with broad "how do I do this" type questions, but I'll try to help in a general sense.
You have the code that draws your car here:
drawCar(80, 340);
drawWheel(45, 410);
Those numbers control where the car shows up. Right now you're always passing in the same values, so the car always shows up in the same place.
What happens if you pass in different values? Try something like this:
drawCar(mouseX, mouseY);
drawWheel(mouseY, mouseX);
This should show your car wherever your mouse is.
Now, if you want to show the care moving around on its own, you probably want to store your state in a set of variables, and then change those variables over time.
Shameless self-promotion: here is a tutorial on animation in Processing.

How can I make window smaller than 100 pixels

I try to make a status bar on the bottom of my screen, but the window can not be made less than 100 pixels. I'm working on Processing 3.0.1.
I use the following code
void setup() {
surface.setResizable(true);
surface.setSize(300, 20);
surface.setLocation(displayWidth-300, displayHeight-50);
}
void draw() {
background(128);
}
any ideas??
Thank you all in advance
J!
If you remove the surface.setResizable(true); statement, you can see the canvas is 300x20, but the window is not:
Processing 3.0 has a lot of changes including refactoring the windowing code, which previously relied on Java's AWT package.
Going through the current source code, you can see:
static public final int MIN_WINDOW_WIDTH = 128;
static public final int MIN_WINDOW_HEIGHT = 128;
defined in PSurface.java line 34
and used through out PSurfaceAWT.java to ensure these minimum window dimensions.
Trying to access the Surface's canvas (println(surface.getNative());) I can it's listed as processing.awt.PSurfaceAWT$SmoothCanvas and I can see a SmoothCanvas class with a getFrame() method which looks promising, but that doesn't seem to be accessible (even though it's a public method of a public class).
So by default, at this time, I'd say resizing the window to be smaller than 128x128 in Processing 3.x is a no go.
If Processing 3.x and smaller window is a must it might be possible to tweak the source code yourself and recompile the core library, but this may bite you later on when having multiple Processing project with multiple versions of the Processing core library. I wouldn't recommend tinkering with the core library normally.
If you can use Processing 2.x for your project, making the window size smaller than 100 pixels is achievable:
import java.awt.Dimension;
int w = 300;
int h = 20;
int appBarHeight = 23;//this is on OSX, on Windows/Linux this may be different
void setup() {
size(w, h);
frame.setResizable(true);
}
void draw() {
if (frame.getHeight() != h+appBarHeight){//wait for Processing to finish setting up it's window dimensions (including minimum window dimensions)
frame.setSize(w,h+appBarHeight);//set your dimensions
}
background(128);
}

SharpGL Animation Questions

So I am writing a program that parses files with xyz points and makes a bunch of connected lines. What I am trying to do is animate each line being drawn. I have tried to use VBO's and Display Lists in order to increase performance (as I am dealing with large amount of data points i.e. 1,000,000 points) but I could not figure out how to use them in SharpGL. So the code I am using to draw right now is as follows:
private void drawInput(OpenGL gl)
{
gl.Begin(OpenGL.GL_LINE_STRIP);
for (int i = 0; i < parser.dataSet.Count; i++)
{
gl.Color((float) i, 3.0f, 0.0f);
gl.Vertex(parser.dataSet[i].X, parser.dataSet[i].Y, parser.dataSet[i].Z);
gl.Flush();
}
gl.End();
}
I know immediate mode is super noobzore5000 of me, but I can't find any SharpGL examples of VBO's or Display Lists. So know what I want to do is to 'redraw' the picture after each line is drawn. I thought when the flush method is called, it draws everything up to that point. But it still 'batches' it, and displays all the data at once, how can I animate this? I am incredibly desperate, I don't think thoroughly learning OpenGL or DirectX is practical for such a simple task.
After lots of tinkering, I chose to go with OpenTK because I did end up figuring out VBO's for SharpGL and the performance is AWFUL compared to OpenTK. I will give an answer as to how to animate in the way that I wanted.
My solution works with Immediate Mode and using VBO's. The main concept is making a member integer (animationCount) that you increase every time your paint function gets called, and paint up to that number.
Immediate Mode:
private void drawInput(OpenGL gl)
{
gl.Begin(OpenGL.GL_LINE_STRIP);
for (int i = 0; i < animationCount; i++)
{
gl.Color((float) i, 3.0f, 0.0f);
gl.Vertex(parser.dataSet[i].X, parser.dataSet[i].Y, parser.dataSet[i].Z);
}
gl.End();
animationCount++;
}
or
VBO:
private void glControl1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
GL.DrawArrays(PrimitiveType.LineStrip, 0, animationCount);
animationCount++;
}

3D SVG animation in Processing—flickering problems, svg loading questions

I'm trying to make a time lapse geographic twitter visualization inspired by Jer Thorp's "Just Landed". I am using the latest version of processing.
I'm using an SVG image for my map because I want to be able to zoom into the map at an arbitrary angle, to focus on certain localities, then show the twitter connections on a global scale. I'm running into several problems, the first of which is a flickering of path boundaries of the countries when I rotate my map. Here's a screenshot of my problem:
Here is my code which is causing the problem:
import processing.opengl.*;
import java.awt.event.*;
PShape map;
PShape test1;
PShape test2;
//camera position/movement intialization
PVector position = new PVector(450, 450);
PVector movement = new PVector();
PVector rotation = new PVector();
PVector velocity = new PVector();
float rotationSpeed = 0.035;
float panningsSpeed = 0.035;
float movementSpeed = 0.05;
float scaleSpeed = 0.25;
float fScale = 2;
void setup(){
map = loadShape("blank_merc.svg"); //swap out for whatever file
size(900, 900, OPENGL);
smooth();
fill(150, 200, 250);
addMouseWheelListener(new MouseWheelListener(){
public void mouseWheelMoved(MouseWheelEvent mwe){
mouseWheel(mwe.getWheelRotation());
}
});
}
void draw(){
if (mousePressed) {
if (mouseButton==LEFT) velocity.add( (pmouseY-mouseY) * 0.01, (mouseX-pmouseX) * 0.01, 0);
if (mouseButton==RIGHT) movement.add( (mouseX-pmouseX) * movementSpeed, (mouseY-pmouseY) * movementSpeed, 0);
}
//TODO: implement reset functionality: DONE
if (keyPressed){
if (key=='r'){
position.set(450,450);
rotation.sub(rotation.get());
velocity.sub(velocity.get());
movement.sub(movement.get());
}
}
velocity.mult(0.95);
rotation.add(velocity);
movement.mult(0.95);
position.add(movement);
background(255);
//lights();
translate(position.x, position.y, position.z);
rotateX(rotation.x*rotationSpeed);
rotateY(rotation.y*rotationSpeed);
scale(fScale);
shape(map,-250,-250,1000,1000);
}
void mouseWheel(int delta){
fScale -= delta * scaleSpeed;
fScale = max(0.5, fScale);
}
I was told it might be z-fighting amongst the paths, and I think this might be the problem because the flickering is more problematic when the map is mid rotation, especially at angles that are non orthogonal to the viewing plane. I tried to remedy this by "translating" a PShape child of the file a small amount in the Z direction with the test1.translate(0,0,0.1); command, but I get an error telling me illegal argument exception: cannot use translate(x,y,z) on a PMatrix2D.
I've also had trouble testing my code with other SVG map files and generally getting the SVG to look like what I think it should look like. There are a bunch of cities and other weird markers on my SVG map, and even when i download the completely "blank" svg world map mercator projection from wikimedia commons. There are these city marker/region attributes which show up in the processing render that dont show up in the browser view. I'm trying to figure out how to "clean" my SVG file up in Inkscape, but I'm unsure what specifically to look for.
For example, I've run it with this map: http://commons.wikimedia.org/wiki/File:Mercator_Projection.svg
but the dots and lines I have no use for, and I'm having to resort to manually selecting and deleting the paths, which is not a very thorough process
and when I use this map, which is supposed to be the "blank version" of the above without all the markers, I see not only a bunch of markers (presumably hidden with some style attribute in the SVG XML?) but also this weird vertical banding, and my camera controls are super slow. The applet appears to be behaving as if the file is way too large, but its like 2MB. Here's a screenshot of what this looks like:
I'm really just looking for a way to get a "clean" SVG world map into Processing so I can spin it around and zoom in on it, and if I can get that to work I can start the Arc-Drawing part. I would sincerely appreciate any assistance anyone could give me.
Thanks
If I understand your question correctly, the flickering is only on the edges, presumably where they overlap. That would suggest z-fighting to me. I usually find that a simple test outside your main sketch is best, just as a quick way to see what's happening and how you might fix it.
If you make a simple SVG with two overlapping shapes, sharing just one edge, does the same thing happen?
If so, I think the easiest solution (though not that easy) would be either:
Select all the countries in Illustrator
Use Object > Transform > Scale... and shrink by a tiny amount
Then share your fixed map for everyone else!

How to draw multiple points on image

When i am using code below to draw points over my image, each time i draw a new point, i am loosing my previous point, though i want to keep that as well.
void imageviewer :: paintEvent(QPaintEvent * e)
{
QLabel::paintEvent(e);
if(mpaintflag)
{
QPainter painter(this);
QPen paintpen(Qt::red);
paintpen.setWidth(10);
QPoint p1;
p1.setX(mFirstX);
p1.setY(mFirstY);
painter.setPen(paintpen);
painter.drawPoint(p1);
}
}
I think that i can keep my previous points using QList, tried a lot, but still no idea how to do it using QList.
I did it, i make a list (QList) to store the coordinate points, and then paint all the points in the list every time on image. That's how i did it.
Thanks

Resources