Transformation: scaling triangle, mouseDragged inside triangle?

I need to

  1. Complete the scale transformation and test it using various values for scaling in (x, y)

  2. Support direct translation (dragging) of the Triangle with the mouse using the following constraints:

a) the user must press the mouse inside the the triangle to drag it. while dragging, the mouse can leave the triangle. Hint: implement inside() virtual function. Note that triangle can be transformed, so you need to take that into account when you write this function.

b) the triangle should track with the mouse. the triangle should always stay at the same relative position to the mouse. For example, if you click and drag inside a corner of the triangle, the mouse should be still in the corner at the same position relative to the triangle after dragging stops.

  1. Support rotation of the triangle around it’s own center (and in any position) using the mouse left button and CTRL modifier key.

  2. Add visual affordances - a) highlight triangle in a different color when mouse moves inside triangle. b) de-highlight when mouse moves outside of triangle. c) change color of triangle when it is selected. d) change back to original color when de-selected.

“”"
#pragma once

#include “ofMain.h”

class Shape {

public :

Shape() {}

virtual void draw() {}

virtual bool inside(glm::vec3 p) {

return true ;

}

glm::mat4 getMatrix() {

glm::mat4 trans = glm::translate(glm::mat4(1.0), glm::vec3(pos));

glm::mat4 rot = glm::rotate(glm::mat4(1.0), glm::radians(rotation), glm::vec3(0, 0, 1));

return (trans * rot);

}

glm::vec3 pos;

float rotation = 0.0; // degrees

vectorglm::vec3 verts;

};

class TriangleShape : public Shape {

public :

TriangleShape() {}

TriangleShape(glm::vec3 p1, glm::vec3 p2, glm::vec3 p3) {

verts.push_back(p1);

verts.push_back(p2);

verts.push_back(p3);

}

void draw();

bool inside(glm::vec3 p) {

return false ;

}

//apply inverts transfer to p point

bool inside(glm::vec3 p, glm::vec3 p1, glm::vec3 p2, glm::vec3 p3) {

glm::vec3 v1 = glm::normalize(p1 - p);

glm::vec3 v2 = glm::normalize(p2 - p);

glm::vec3 v3 = glm::normalize(p3 - p);

float a1 = glm::orientedAngle(v1, v2, glm::vec3(0, 0, 1));

float a2 = glm::orientedAngle(v2, v3, glm::vec3(0, 0, 1));

float a3 = glm::orientedAngle(v3, v1, glm::vec3(0, 0, 1));

if (a1 < 0 && a2 < 0 && a3 < 0) return true;

else return false ;

}

//------

};

class ofApp : public ofBaseApp{

public :

void setup();

void update();

void draw();

void keyPressed(int key);

void keyReleased(int key);

void mouseMoved(int x, int y );

void mouseDragged(int x, int y, int button);

void mousePressed(int x, int y, int button);

void mouseReleased(int x, int y, int button);

void mouseEntered(int x, int y);

void mouseExited(int x, int y);

void windowResized(int w, int h);

void dragEvent(ofDragInfo dragInfo);

void gotMessage(ofMessage msg);

TriangleShape tri;

};
“”"

“”"
#include “ofApp.h”

void TriangleShape::draw() {

ofSetColor(ofColor::lightBlue);

ofFill();

ofPushMatrix();

ofMultMatrix( getMatrix() );

ofDrawTriangle(verts[0], verts[1], verts[2]);

ofPopMatrix();

}

//--------------------------------------------------------------

void ofApp::setup(){

tri = TriangleShape(glm::vec3(-50, 50, 0), glm::vec3(50, 50, 0), glm::vec3(0, -50, 0));

tri.pos = glm::vec3(ofGetWindowWidth()/2.0-100, ofGetWindowHeight()/2.0-100, 0);

}

//--------------------------------------------------------------

void ofApp::update(){

}

//--------------------------------------------------------------

void ofApp::draw(){

tri.draw();

}

//--------------------------------------------------------------

void ofApp::keyPressed(int key){

}

//--------------------------------------------------------------

void ofApp::keyReleased(int key){

}

//--------------------------------------------------------------

void ofApp::mouseMoved(int x, int y ){
}

//--------------------------------------------------------------

void ofApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------

void ofApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------

void ofApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------

void ofApp::mouseEntered(int x, int y){

}

//--------------------------------------------------------------

void ofApp::mouseExited(int x, int y){

}

//--------------------------------------------------------------

void ofApp::windowResized(int w, int h){

}

//--------------------------------------------------------------

void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------

void ofApp::dragEvent(ofDragInfo dragInfo){

}
“”"

Always nice to see the single Tri situation,

Yours is very elegant and concise, resorting to glm. Mine is lengthier, as I have implemented every single math op at 1D level, though similar. to what you impl, Check it out, at ~21%, just for the laughs of it. Like your inside virt fun, mine always keeps returning true. it’s nice, a different projection, anywhere you screenclick you have to transform the tri in order for it to ve inside, reverse shadow interpol and similar. Right now i am trying these projective transforms edhat.com/en/blog/making-open-source-more-inclusive-eradicating-problematic-language, in the end its still an open sea, i’d also keep a list of transformed verts, just to optimize a attiny