I’m trying to draw a perpendicular line to two given vectors and I can’t work it out for the life of me. I suppose what I’m receiving with atan (or PVector.getAngle) is not what I was expecting… ie a degree returned within 360 degrees.
size(400,400)
nx1, ny1 = 200,100
nx2, ny2 = 100,300
noFill()
strokeWeight(3)
stroke(255,0,0)
line(nx1,ny1, nx2, ny2)
nx3 = nx2-((nx2-nx1)/2)
ny3 = ny2-((ny2-ny1)/2)
ellipse(nx3,ny3,20,20)
stroke(255,255,0)
a = atan2(ny2-ny1, nx2-nx1)
print "angle", a
print "angle deg", degrees(a)
r = 100 # max addition push
a-=90
linex = r * cos(a)
liney = r * sin(a)
line(nx3, ny3, nx3+linex, ny3+liney)
we cannot simply use traditional addition/multiplication/etc. Instead, we’ll need to do some “vector” math, which is made easy by the methods inside the PVector class.
what you prove wrong AND refs to missing methods list.
but in PYTHON i did not know how to do so i had to invent a ‘vline’ method
In Python you can define a method in such a way that there are multiple ways to call it.
Given a single method or function, we can specify the number of parameters ourself.
Depending on the function definition, it can be called with zero, one, two or more parameters.
Python, just like JS and many other languages, functions are stored in variables too.
It’s not like in Java where we can have a variable/field/parameter, a function/method and a class/interface sharing the same name within the same context:
void setup() {
println(same(same) * new same().same); // 60
exit();
}
char same = 10;
int same(int same) {
return same / 3;
}
class same {
short same = 20;
}
For our convenience, most of Processing’s API is made available to us under Python Mode via global variables.
That is, size, background, color, triangle, etc. are all global variables, having a function reference as their value.
However, when we use def, class or even the assign = operator in the global context, if the label already exists, we end up reassigning its old value w/ a new 1.
For example, global variable line already exists holding a reference to method PApplet::line().
When we issue def line():, we replace the old Java method w/ a new Python 1.
But even after that, not all is lost, b/c we still got another global variable named this, which holds a reference to current PApplet sketch instance.
And we can still invoke the original PApplet::line() this way: this.line().
However, once a PApplet member is globally replaced, only via global variable this in order to reach it again.