How to calculate the tangent line of a circle?
in
Programming Questions
•
2 years ago
Hi,
I just dont seem to get this trig problem, so I need your help, I need to calculate the tangent line for each vertex of a circle, liket show in the image
And this is the code
- int numSegments = 8;
- float radius = 80;
- float slope;
- PVector[] vectors = new PVector[numSegments];
- void setup() {
- size(300, 300);
- smooth();
- noLoop();
- // Get cirlce vertices
- float k = TWO_PI / (float)numSegments;
- for ( int i = 0; i < numSegments; i++ ) {
- float x = cos(k*i) * radius;
- float y = sin(k*i) * radius;
- vectors[i] = new PVector(x, y);
- }
- }
- void draw() {
- background(255);
- // Get slope at point
- float m = (vectors[1].y - vectors[0].y) / (vectors[1].x - vectors[0].x);
- slope = pow(-m, -1);
- println("slope: " + slope );
- float b = vectors[1].y - slope*(vectors[1].x);
- println("y intercept: " + b);
- // Draw circle
- pushMatrix();
- translate( width/2, height/2 );
- for ( int i = 0; i < numSegments; i++ ) {
- float x = vectors[i].x;
- float y = vectors[i].y;
- float x1 = (i < numSegments-1) ? vectors[i+1].x : vectors[0].x;
- float y1 = (i < numSegments-1) ? vectors[i+1].y : vectors[0].y;
- stroke(0); noFill();
- strokeWeight(2);
- line(x, y, x1, y1);
- strokeWeight(1);
- stroke(127);
- line(0, 0, x, y);
- fill(127);
- textAlign(CENTER);
- text("V" + i, x * 1.2, y * 1.2 + 5);
- }
- popMatrix();
- }
Any help will be much appreciated
- rS
1