Why am I getting these errors when I try to compile my sketch in Arduino UNO? - arduino-uno

Error messages:
Arduino: 1.8.12 (Windows Store 1.8.33.0) (Windows 10), Board: "Arduino
Uno"
C:\Users\ghane\AppData\Local\Temp\ccVLh1df.ltrans0.ltrans.o: In
function `build':
C:\Users\ghane\OneDrive\Documents\Arduino\libraries\tinn-master/test.c:122:
undefined reference to `fopen'
C:\Users\ghane\AppData\Local\Temp\ccVLh1df.ltrans0.ltrans.o: In
function `lns':
C:\Users\ghane\OneDrive\Documents\Arduino\libraries\tinn-master/test.c:37:
undefined reference to `rewind'
C:\Users\ghane\AppData\Local\Temp\ccVLh1df.ltrans0.ltrans.o: In
function `xtsave':
C:\Users\ghane\OneDrive\Documents\Arduino\libraries\tinn-master/Tinn.c:133:
undefined reference to `fopen'
C:\Users\ghane\AppData\Local\Temp\ccVLh1df.ltrans0.ltrans.o: In
function `xtload':
C:\Users\ghane\OneDrive\Documents\Arduino\libraries\tinn-master/Tinn.c:145:
undefined reference to `fopen'
collect2.exe: error: ld returned 1 exit status
exit status 1 Error compiling for board Arduino Uno.
CODE:
extern "C" {
#include "Tinn.h"
};
#define IN1 1 //left motor
#define IN2 2 //left motor
#define IN3 4 //right motor
#define IN4 6 //right motor
#define LMOTOR 3 //left motor
#define RMOTOR 5 //left motor
#define RLED 7 //red led
#define YLED 8 //yellow led
#define GLED 12 //green led
#define Trig1 A0 //front uss sent
#define Echo1 A1 //front uss received
#define Trig2 A2 //left uss sent
#define Echo2 A3 //left uss received
#define Trig3 A4 //right uss sent
#define Echo3 A5 //right uss received
long duration, MyUSsensor1, MyUSsensor2, MyUSsensor3, y1, y2, y3;
int initial_motor_speed = 150;
int mtoutput;
float out;
float x1;
float x2;
float biases[] = {
0.375973, 0.226676
};
float weights[] = {
2.000090, -2.732119, -1.512398, 2.352158, -1.499895, -2.407945,
-3.524677, 1.051439, 4.120766, 0.006929, -2.855277, 0.514731,
5.028436, 1.573380, -4.188428, -1.534567, -1.662638, 1.460011,
-0.469579, -3.754514, 1.828197, 1.151905, -3.960257, -0.032369,
-4.076006, -4.283566, 6.279175, -2.485125, 1.049302, -1.357511,
-4.136541, -3.860311, -3.208045, 0.286115, -1.206529, -3.035516,
7.187942, -3.311538, -5.129674, -4.287395
};
Tinn tinn ;
void setup() {
// Setup the neural network
tinn = xtbuild(3, 8, 2);
for (int i = 0; i < tinn.nb; i++)
tinn.b[i] = biases[i];
for (int i = 0; i < tinn.nw; i++)
tinn.w[i] = weights[i];
pinMode(LMOTOR, OUTPUT); // PWM for Left Motor
pinMode(RMOTOR, OUTPUT); // PWM for Right Motor
pinMode(IN1, OUTPUT); // Control for Left Motor 1
pinMode(IN2, OUTPUT); // Control for Left Motor 2
pinMode(IN3, OUTPUT); // Control for Right Motor 1
pinMode(IN4, OUTPUT); // Control for Right Motor 2
pinMode (RLED, OUTPUT); //Control for rled
pinMode (YLED, OUTPUT); //Control for yled
pinMode (GLED, OUTPUT); //Control for gled
Serial.begin (9600);
pinMode (Trig1, OUTPUT); //control for front uss sent
pinMode (Echo1, INPUT); //control for front uss received
pinMode (Trig2, OUTPUT); //control for left uss sent
pinMode (Echo2, INPUT); //control for left uss received
pinMode (Trig3, OUTPUT); //control for right uss sent
pinMode (Echo3, INPUT); //control for right uss received
digitalWrite(IN1, HIGH); //Initially Moving Forward
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
digitalWrite (RLED, LOW); //Initially no light
digitalWrite (YLED, LOW);
digitalWrite (GLED, LOW);
analogWrite(LMOTOR, initial_motor_speed); //Left Motor Speed
analogWrite(RMOTOR, initial_motor_speed); //Right Motor Speed
}
float get_ultrasensor (int trigPin, int echoPin) {
digitalWrite (trigPin, LOW);
delayMicroseconds(30);
digitalWrite (trigPin, HIGH);
delayMicroseconds (100);
digitalWrite(trigPin , LOW);
duration = pulseIn(echoPin, HIGH);
long distance = (duration / 2) / 29.1;
return distance;
}
float* predict_output() {
float NN_input[3] = { 0.0, 0.0, 0.0 };
NN_input[0] = y1;
NN_input[1] = y2;
NN_input[2] = y3;
return xtpredict(tinn, NN_input);
}
float integer(float value) {
if (value < 10) {
return 1.0;
}
else {
return 0.0;
}
}
void motor_output() {
if (x1 <= 0.5 && x2 <= 0.5) {
mtoutput = 0;
}
else if (x1 < 0.5 && x2 >= 0.5) {
mtoutput = 3;
}
else if (x1 >= 0.5 && x2 <= 0.5) {
mtoutput = 2;
}
else if (x1 >= 0.5 && x2 >= 0.5) {
mtoutput = 1;
}
if (mtoutput == 1) {
digitalWrite (RLED, HIGH); //active rled
analogWrite(LMOTOR, 0); //Stop the motor
analogWrite(RMOTOR, 0);
delay(1000); //Delay 1 second
digitalWrite (RLED, LOW); //deactive rled
digitalWrite(IN1, LOW); //Reverse //0
digitalWrite(IN2, HIGH); //1
digitalWrite(IN3, LOW); //0
digitalWrite(IN4, HIGH); //1
delay(50);
analogWrite(LMOTOR, 150); //reverse
analogWrite(RMOTOR, 150);
delay(2000); //Delay 2 seconds
analogWrite(LMOTOR, 0); //stop
analogWrite(RMOTOR, 0);
digitalWrite(IN1, LOW); //Perform U-Turn on current position
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(50);
analogWrite(LMOTOR, 150);
analogWrite(RMOTOR, 150);
delay(4000); //Delay time need to change accordingly
digitalWrite(IN1, HIGH); //Moving Forward
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(50);
}
//RIGHT CORNER DETECTED, AVOID RIGHT CORNER
else if (mtoutput == 2) {
digitalWrite (GLED, HIGH); //active gled
analogWrite(LMOTOR, 0);
analogWrite(RMOTOR, 0);
delay(1000);
digitalWrite (GLED, LOW); //deactive gled
//2.TURN LEFT
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(50);
analogWrite(LMOTOR, 150);
analogWrite(RMOTOR, 150);
delay(2000);
//3.MOVE FORWARD
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(50);
delay(2000);
//4.TURN RIGHT
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(50);
delay(2000);
//5.MOVE FORWARD
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(50);
delay(2000);
//6.TURN RIGHT
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(50);
delay(2000);
//7.MOVE FORWARD
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(50);
delay(2000);
//8.TURN LEFT AND MOVE FORWARD
digitalWrite(IN1, HIGH); //TURN LEFT
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(50);
delay(2000);
digitalWrite(IN1, HIGH); //MOVE FORWARD
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(50);
delay(2000);
}
//LEFT CORNER DETECTED, AVOID LEFT CORNER
else if (mtoutput == 3) {
digitalWrite (YLED, HIGH); //active yled
analogWrite(LMOTOR, 0);
analogWrite(RMOTOR, 0);
delay(1000);
digitalWrite (YLED, LOW); //deactive yled
//2.TURN RIGHT
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(LMOTOR, 150);
analogWrite(RMOTOR, 150);
delay(50);
//3.MOVE FORWARD
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(50);
delay(2000);
//4.TURN LEFT
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(50);
delay(2000);
//5.MOVE FORWARD
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(50);
delay(2000);
//6.TURN LEFT
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(50);
delay(2000);
//7.MOVE FORWARD
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(50);
delay(2000);
//8.TURN RIGHT AND MOVE FORWARD
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(50);
delay(2000);
digitalWrite(IN1, HIGH); //MOVE FORWARD
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(50);
delay(2000);
}
else {
analogWrite(LMOTOR, 150);
analogWrite(RMOTOR, 150);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(50);
delay(2000);
}
}
void loop() {
MyUSsensor1 = get_ultrasensor(Trig1, Echo1);
Serial.println (MyUSsensor1);
MyUSsensor2 = get_ultrasensor(Trig2, Echo2);
Serial.println (MyUSsensor2);
MyUSsensor3 = get_ultrasensor(Trig3, Echo3);
Serial.println (MyUSsensor3);
y1 = integer(MyUSsensor1);
y2 = integer(MyUSsensor2);
y3 = integer(MyUSsensor3);
float *out = predict_output();
out[0] = x1;
out[1] = x2;
motor_output();
}

The files containing the libraries have to be in the same folder as the .ino file. Upon doing this, no error messages were found.

Related

How to execute a function only once in voice draw(); but still have it displayed for the whole animation

I'm struggling with something here.
Basically I made an animation with several functions. I want the functions to be animated, except for one that I want to be still.
If I specify noLoop, then nothing gets animated, if I tell the function to run a set number of times, then after that number it stops being displayed. What I want is to have it run once but then be still displayed.
Do you guys have any idea of how I could do that ?
Here is the code :
int r1,r2,r3 = 0;
int i1, i2,i3;
void setup(){
size(800,800);
background(255,0);
//noLoop();
}
void draw(){
background(255,0);
rosace();
croix();
sillon1();
sillon2();
}
void rosace(){
for (i1 = 0;i1<230; i1++){
rectMode(CENTER);
noFill();
stroke(20);
strokeWeight(1);
pushMatrix();
translate(width/2,height/2);
translate(400,400);
rotate(radians(r1));
rect(0,0,400,400);
r1 +=1;
//println(r);
popMatrix();
println("rosace ex");
}
}
void croix(){
pushMatrix();
strokeWeight(2);
stroke(0);
translate(width/2, height/2);
rotate(radians(45));
line(-10,0,10,0);
line(0,-10,0,10);
popMatrix();
}
void sillon1(){
for (i2 =0; i2<360; i2++){
pushMatrix();
translate(width/2,height/2);
strokeWeight(int(random(0,7)));
rotate(radians(r2));
point(-330,0);
popMatrix();
r2 +=1;
}
}
void sillon2(){
for (i2 =0; i2<2000; i2++){
pushMatrix();
translate(width/2,height/2);
strokeWeight(int(random(1,3)));
rotate(radians(r2));
point(-360,0+(random(-2,2)));
popMatrix();
r2 +=1;
}
}
The void rosace(); is the one that I'd like to be non-animated.
The problem is that you're storing r1 as a global variable. This means that it keeps it's value after every iteration.
Instead, make it a local variable:
void rosace() {
int r1 = 0;
for (i1 = 0; i1<230; i1++) {
rectMode(CENTER);
noFill();
stroke(20);
strokeWeight(1);
pushMatrix();
translate(width/2, height/2);
translate(400, 400);
rotate(radians(r1));
rect(0, 0, 400, 400);
r1 +=1;
popMatrix();
println("rosace ex");
}
}
You can't just keep a part of the scene, when animations are drawn on top of it. You have to draw the entire scene in every frame. Ensure that rosace draws the same in every frame.
Just set r1 = 0 in every frame, then the algorithm in rosace generates the same rectangles in every frame:
void rosace(){
r1 = 0;
for (i1 = 0;i1<230; i1++){
// [...]
}
}

Processing - Move circle with cursor position

I made a simple drawing program to draw lines and increase/decrase the thickness of the line:
float strokeWeight = 2;
void setup() {
size(640, 360);
noSmooth();
fill(126);
background(255);
strokeWeight(strokeWeight);
}
void draw() {
background(0);
ellipse(mouseX, mouseY, strokeWeight/2, strokeWeight/2);
background(255);
if (mousePressed) {
stroke(0);
line(mouseX, mouseY, pmouseX, pmouseY);
}
if (keyPressed) {
if (key == '+') {
strokeWeight = strokeWeight + 0.5;
}
if (key == '-') {
strokeWeight = strokeWeight - 0.5;
}
if (strokeWeight >= 0.5) {
strokeWeight(strokeWeight);
}
}
}
Now I want to move a circle with my cursor that indicates the current thickness of the line. I tried something like this:
ellipse(mouseX, mouseY, strokeWeight/2, strokeWeight/2)
But this way it draws ellipses over and over again. Is there a way to "erase" the circle made before?
I am not 100% sure that I've understood your question, but you probably want to use PGrahics, on one you keep the lines, on the other you draw the circle.
float strokeWeight = 2;
PGraphics canvas;
PGraphics thickness_circle;
void setup() {
size(640, 360);
canvas = createGraphics(width, height);
thickness_circle = createGraphics(width, height);
thickness_circle.beginDraw();
thickness_circle.noFill();
thickness_circle.strokeWeight(1);
thickness_circle.stroke(255, 0, 0);
thickness_circle.endDraw();
}
void draw() {
background(255);
if (keyPressed) {
if (key == '+') {
strokeWeight += 0.5;
}
if (key == '-') {
strokeWeight -= 0.5;
}
strokeWeight = strokeWeight >= 0.5 ? strokeWeight : 0.5;
}
if (mousePressed) {
canvas.beginDraw();
canvas.strokeWeight(strokeWeight);
canvas.line(mouseX, mouseY, pmouseX, pmouseY);
canvas.endDraw();
}
image(canvas, 0, 0);
thickness_circle.beginDraw();
thickness_circle.clear();
thickness_circle.ellipse(mouseX, mouseY, strokeWeight, strokeWeight);
thickness_circle.endDraw();
image(thickness_circle, 0, 0);
}

Using pushMatrix(), popMatrix(), translate(), and rotate()

I have a program that draws a flower with 4 petals to the screen where ever you click. Instead of redoing the petals one at a time I was just going to attempt to rotate the flower by a quarter turn and have it overlay the first flower, so it appears to have 8 petals. The problem is that it draws the second flower some distance away from the first in the x and y direction, but as I get closer to (0,0) they get closer to overlapping (and do at (0, 0)). I'm not sure why this is happening and would appreciate any help. I'm using Processing 3 for this program.
int c_center = 15;
int c_petal = 20;
int petalsize = 70;
color rcol;
void setup(){
fullScreen();
background(0);
}
void draw(){
rcol = color(random(255), random(255), random(255));
}
void mouseClicked(){
pushMatrix();
flower();
translate(mouseX, mouseY);
rotate(PI/4);
flower();
popMatrix();
}
void flower(){
//left petal
make_petal(mouseX - (petalsize - c_petal), mouseY, petalsize, c_petal);
//right petal
make_petal(mouseX + (petalsize - c_petal), mouseY, petalsize, c_petal);
//top petal
make_petal(mouseX, mouseY - (petalsize - c_petal), c_petal, petalsize);
//bottom petal
make_petal(mouseX, mouseY + (petalsize - c_petal), c_petal, petalsize);
//flower center
fill(random(255), random(255), random(255));
ellipse(mouseX, mouseY, c_center, c_center);
}
void make_petal(int a, int b, int c, int d){
fill(rcol);
ellipse(a, b, c, d);
}
You're mixing two styles of drawing together: your flower() function uses mouseX and mouseY to determine where to draw the petals, but then you're also calling translate(mouseX, mouseY);. So you're drawing petals relative to the mouse, but then moving the entire canvas by the mouse position, and then drawing more petals relative to the mouse again.
Instead, you probably want to only use one style of drawing. Specifically, you probably don't want your flower() function to care about the mouse position. Treat it like a "stamp" that draws things around the origin at 0,0. Then move the origin by calling translate() and rotate the stamp by calling rotate().
I corrected the code thanks to feedback, so it works now in case anyone is interested. I condensed it a little by just making one petal then rotating it through a loop.
int c_center = 20;
int c_petal = int(random(5, 15));
int petalsize = int(random(30, 60));
color rcol;
void setup(){
fullScreen();
background(0);
}
void draw(){
rcol = color(random(255), random(255), random(255));
}
void mouseClicked(){
pushMatrix();
translate(mouseX, mouseY);
for(int r = 0; r < 360; r+=5){
rotate(radians(r));
flower();
}
popMatrix();
}
void flower(){
//petal
make_petal(0, 0 - (petalsize - c_petal), c_petal, petalsize);
//flower center
fill(random(255), random(255), random(255));
ellipse(0, 0, c_center, c_center);
}
void make_petal(int a, int b, int c, int d){
fill(rcol);
ellipse(a, b, c, d);
}

Trying to get the Lives and Game Over portion of my game to work

I'm creating a collision detection game where:
Every time I hit the wall, the number of my lives go down.
Once I get to 0 lives, the game is over.
But the game is letting me go into negative lives. Also, my click to begin once you win doesn't seem to work either... Does anybody know how to fix this?
PImage startScreen;
int gamestate=1;
int lives = 3;
class Sprite {
float x;
float y;
float dx;
float dy;
}
Sprite rect=new Sprite();
Sprite ball=new Sprite();
void setup(){
size(500,500);
rect.x = 500;
rect.y = 12;
ball.y= mouseY;
background(0);
fill(0,255,0);
text("Click to Begin", 10, 250);
}
void draw(){
if(gamestate ==0){
background(0);
fill(0, 255,0);
noStroke();
rect(0,235, 500,2.5);
rect(0,250, 500,2.5);
fill(0);
rect(0,238,rect.x,rect.y);
fill(0,255,0);
ellipse(mouseX, mouseY, 2,2);
text("lives left:"+lives, 10, 20);
if (mouseY<240 || mouseY>247){
background(0);
lives = lives-1;
if(lives <= 0){
text("Game Over. \nClick to Begin", 225,250);
gamestate=1;
}
}
if (mouseX >= 495){
background(0);
text("You Win! \nClick to Begin Again.", 225,250);
}
}
}
void mousePressed(){
if (gamestate ==1){
gamestate=0;
}
}
Keep in mind that before your mouse enters the sketch for the first time, mouseX and mouseY are both 0. So in your draw() function, when you check if mouseY < 240, that's true. You do that 60 times per second, so you lose all 3 of your lives right away.
To fix this, you might want to have a "starting rectangle" that the player has to click to start the game, that way you know the mouse is in the window.
Then after that, you have to give the player a chance to get back to the starting circle before starting the next round, otherwise you just keep losing lives 60 times per second.
That basics might look something like this:
int gamestate=1; //1 is start screen, 0 is playing, 2 is between lives, 3 wins
int lives = 3;
class Sprite {
float x;
float y;
float dx;
float dy;
}
Sprite rect=new Sprite();
Sprite ball=new Sprite();
void setup() {
size(500, 500);
rect.x = 500;
rect.y = 12;
ball.y= mouseY;
}
void draw() {
background(0);
if (gamestate ==0) {
fill(0, 255, 0);
noStroke();
rect(0, 235, 500, 2.5);
rect(0, 250, 500, 2.5);
fill(0);
rect(0, 238, rect.x, rect.y);
fill(0, 255, 0);
ellipse(mouseX, mouseY, 2, 2);
text("lives left:"+lives, 10, 20);
if (mouseY<240 || mouseY>247) {
lives = lives-1;
gamestate=2;
if (lives <= 0) {
text("Game Over. \nClick to Begin", 225, 250);
gamestate=1;
}
}
if (mouseX >= 495) {
gamestate=3;
}
}
else if(gamestate == 1 || gamestate==2){
fill(0, 255, 0);
text("Click to Begin", 10, 230);
rect(0, 240, width, 7);
}
else if(gamestate == 3){
text("You Win! \nClick to Begin Again.", 225, 250);
}
}
void mousePressed() {
if (gamestate ==1 || gamestate == 2) {
if(mouseY>240 && mouseY<247){
gamestate=0;
}
}
}
Note that I don't really understand what your game is supposed to do: your "safe area" is only 7 pixels tall, which seems pretty small. But assuming this is just an example, my answer should generalize to your real code:
Split your game up into "modes". You started to do this with your gamestate variable, but you're also mixing event code and drawing code. Instead, make every mode a state: the start screen, the playing screen, the "in between lives" screen, the game over screen. Only draw the stuff for that mode, and then change the mode based on input. Basically, instead of checking for "play mode" and then drawing "game over" when you run out of lives, just switch to "game over mode" and let that part of the code draw the "game over" to the screen.

Setting (0,0) to bottom-left without switching X- and Y-axis in Processing(.org)

I'd like to change the coordinate system in a 2d processing sketch, putting (0,0) in the bottom-left rather than the top-left. The following code will move (0,0) to the bottom-left:
transform(0, height);
rotate(radians(-90));
However, it also makes the X-axis the vertical axis. Is there a simple way to move (0,0) to the bottom-left and keep the X-axis horizontal?
One option I've considered is using P3D with a combination of rotate and rotateY, but would prefer a solution for the 2d case.
Thanks for any help!
You could simply translate without rotating:
transform(0, height);
And handle your coordinates as flipped:
boolean useVFlip = true;
void setup(){
size(400,400);
}
void draw(){
background(255);
if(useVFlip) translate(0,height);
drawAxes(100);
translate(width * .5, useVFlip ? (height-mouseY)*-1 : mouseY);
triangle(0,0,100,0,100,100);
}
void keyPressed(){ useVFlip = !useVFlip; }
void drawAxes(int size){
pushStyle();
strokeWeight(10);
stroke(192,0,0);
line(0,0,size,0);
stroke(0,192,0);
line(0,0,0,size);
popStyle();
}
If it makes it easier to get flipped coordinates, you could flip the whole coordinate system using the scale() method:
boolean useVFlip = true;
void setup(){
size(400,400);
}
void draw(){
background(255);
if(useVFlip){
scale(1,-1);
translate(0,-height);
}
drawAxes(100);
translate(width * .5, useVFlip ? height-mouseY : mouseY);
triangle(0,0,100,0,100,100);
}
void keyPressed(){ useVFlip = !useVFlip; }
void drawAxes(int size){
pushStyle();
strokeWeight(10);
stroke(192,0,0);
line(0,0,size,0);
stroke(0,192,0);
line(0,0,0,size);
popStyle();
}
You could do the same with a PMatrix2D, but not sure how familiar your are with them:
boolean useCustomCS = true;
PMatrix2D customCS;
void setup(){
size(400,400);
customCS = new PMatrix2D( 1, 0, 0,
0, -1,height);
fill(0);
}
void draw(){
background(255);
if(useCustomCS) applyMatrix(customCS);
drawAxes(100);
translate(width * .5, useCustomCS ? height-mouseY : mouseY);
text("real Y:" + mouseY + " flipped Y: " + (height-mouseY),0,0);
triangle(0,0,100,0,100,100);
}
void keyPressed(){ useCustomCS = !useCustomCS; }
void drawAxes(int size){
pushStyle();
strokeWeight(10);
stroke(192,0,0);
line(0,0,size,0);
stroke(0,192,0);
line(0,0,0,size);
popStyle();
}
Try the following:
translate(0,height);
scale(1,-1);

Resources