We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hi! I need to draw a point on a border of a rectangle. The point should be located on border at place where mouse is pointing (so on line between rect center and mouse pointer). I need to use trigonometry as rect will be rotating. Below the code which i was able to produce squeezing my math skills to the limit ;) It enables me to draw point but only on south border... then point leaves to +-infinity. i think i need to switch phase by 90 degrees somehow but i don't know how... any help here? thanks :)
int size = 200;
void setup() {
  size(1200,500);
  smooth();
  ellipseMode(CENTER);
  rectMode(CENTER);
}
void draw() {
  background(255);
  noFill();
  PVector center = new PVector(width/2,height/2);
  PVector mouse = new PVector(mouseX, mouseY);
  float mouseDistance = PVector.dist(center,mouse);
  mouse.sub(center);
  float t = atan2(mouse.y, mouse.x);
  float borderDistance = (size/2) / sin(t);  
  float bx = cos(t) * borderDistance; 
  float by = sin(t) * borderDistance;
  pushMatrix(); 
    translate(center.x, center.y);
    strokeWeight(5);
    stroke(255,0,0); 
    point(0,0);
    point(mouse.x, mouse.y);
    point(cos(t)*size/2, sin(t)*size/2);
    point(bx,by);
    strokeWeight(1);
    stroke(0);
    ellipse(0, 0, size, size);
    rect(0,0, size, size);
    stroke(0,255,0); 
    line(0, 0, mouse.x, mouse.y);
  popMatrix();
}
Answers
I suggest you first implement the rotating rectangle. For your purpose you probably want a rotation method that will give you four coordinates for "rotated points" of the rectangle. Then you can use those points to do four line-line intersection checks, between center-mouse line versus each side of the rectangle.
hi Amnon, thanks for answer, i have managed to draw point on border of a rectangle, you can see sketch here. Unfortunately when i make rectangle to rotate then the mouse vector is being rotated as well... not sure how i could correct it though...
Given you existing sketch it's probably easiest to just correct the mouse coordinates. There are also a few other optimizations possible with regard to using .set instead of creating new PVector's and use else if.
Adapted Code