Categories
C++

Task 3 – write program 2 :using program1 as a guide, sort the results in reverse order.

Learning Goal: I’m working on a c++ multi-part question and need an explanation and answer to help me learn.
Rubric:
Plan 10 points: Provide in your solution folder a Flowchart, pseudocode, photo of your hand-written plan, or type-written algorithm about how you plan to solve the below problem.
Filename: UNIT6_Plan_YourLastName.docx
Include programs as requested below.

Deliverable: Zip file of entire solution Folder including the planning file.
Recall that an array is a list of information of the same data type. There are various ways to sort data in programming.
Bubble sort is one such technique that sorts numbers in a list by comparing them 2 at a time and swapping if necessary.
Bubble sort can go Ascending (A to Z. least to greatest), or Descending (Z to A greatest to least)
Study bubble sort here: https://visualgo.net/en/sorting?slide=1 (Links to an external site.)
Be sure to add the attached .h to your .cpp file. DO NOT re-write the array…It lives in the header file.
#include “accounts.h”
Also, refer to the “maxAccounts” variable from the file.
Task:
Create a Folder on your saving location called “Unit6_YourLastName_Task_X” where “X” is replaced by the respective task number below.
Background – Use the provided accounts.h header file in your new program. Open accounts.h and note its contents.
#include “accounts.h”
Write a function that will sort the accountBalances array using the bubble sort ASC algorithm.
Display the cpuTime used.
//Notes
//Using the cpuTime()
//start of program
cout << "Start: " << cpuTime() << endl;
//end of program
cout << "End " << cpuTime() << endl; //document your end time for our Results.txt file.
Create a file called Results.txt. You will use this file to log/write cpuTime() information for the four runs of the program below.
Helpful Reference: Passing Arrays to Functions – Dr_T
https://repl.it/@TysonMcMillan/PassingCPPArraysToFunctionDrT (Links to an external site.)
Task 1 – Write a pseudocode description and a flowchart for the task in "Background" item 2. What is the process for sorting the array with Bubble Sort ASC. Document this step by step process.
Unit6_Pseudocode_Program1_Yourlastname
Unit6_Flowchart_Program1_Yourlastname
Task 2 – Write Program 1 :Write a program that will do the task defined in your pseudocode:
UNIT6_Program1_YourLastName.cpp (or main.cpp)
Use Bubble sort to sort the array ASC
Print the array output to the screen using: void printArray(int array[], int size);
Run the program 3 times
Write/type your results in the Results.txt file with the label "Bubble Sort ASC"
Task 3 – Write Program 2 :Using program1 as a guide, sort the results in reverse order.
UNIT6_Program2_YourLastName.cpp (or main.cpp)
Use Bubble sort to sort the array DESC
Print the array output to the screen using: void printArray(int array[], int size);
Run the program 3 times
Write/type your results in the Results.txt file with the label "Bubble DESC"
Task 4 – Write Program 3 :Using program1, create an additional function for sorting that uses the standard sort function instead of the bubble sort and call this new method for sorting. See http://www.cplusplus.com/articles/NhA0RXSz/ (Links to an external site.)
How does built-in sort work?
#include //to use the sort command
int main() {
int myArray[7] = {1,2,3,4,5,6,7};
sort(myArray,myArray+7); //sort(nameOfArray,nameOfArray+sizeOfArray); //ASC
}
Be reminded that we know the name of or array is accountsBalances and the size of our array is stored in maxAccounts
sort(accountBalances, accountBalances+maxAccounts);
UNIT6_Program3_YourLastName.cpp (or main.cpp)
Use built-in sort function to sort the array ASC
Print the array output to the screen using: void printArray(int array[], int size);
Run the program 3 times
Write/type your results in the Results.txt file with the label “Sort Built-in ASC”
Task 5 – Write Program 4 :Using program1, create an additional function for sorting that uses the standard sort function instead of the bubble sort and call this new method for sorting.
(Links to an external site.)
How does built-in sort work in reverse (DESC)?
#include //to use the sort command
int main() {
int myArray[7] = {1,2,3,4,5,6,7};
sort(myArray,myArray+7); //sort(nameOfArray,nameOfArray+sizeOfArray); //ASC
reverse(myArray,myArray+7); //call sort first, then reverse the order DESC
}
Be reminded that we know the name of or array is accountBalances and the size of our array is stored in maxAccounts
sort(accountBalances, accountBalances+maxAccounts);
reverse(accountBalances, accountBalances+maxAccounts);
UNIT6_Program4_YourLastName.cpp (or main.cpp)
Use built-in sort function to sort the array DESC
Print the array output to the screen using: void printArray(int array[], int size);
Run the program 3 times
Write/type your results in the Results.txt file with the label “Sort Built-in DESC”
Task 6 – Finally, in Results.txt write a brief summary about the average speed of each program run. Which program/process ran the fastest on average?
Task 7 – Using zip software, Zip the Entire Unit6_YourLastName folder naming the resulting file Unit6_YourLastName.zip
How to Compress or Uncompress a file in Windows (Links to an external site.)
Instructions for Submission:
Upload your UNIT6_YourLastName.zip file
In the “written submission/comments” Provide Dr_T the Live URL share of1) Repl.it share URL(4), and remember to include your planning file and the Result.txt file as well. 2) the URL of your GitHub repository If using an IDE other than Repl.it, provide Dr_T with the zip file submission of your code.
Attachments:
accounts.h : https://inst-fs-iad-prod.inscloudgate.net/files/79…

Categories
C++

Sort(accountbalances, accountbalances+maxaccounts);

Learning Goal: I’m working on a c++ question and need an explanation and answer to help me learn.
Plan 10 points: Provide in your solution folder a Flowchart, pseudocode, photo of your hand-written plan, or type-written algorithm about how you plan to solve the below problem.
Filename: UNIT6_Plan_YourLastName.docx
Include programs as requested below.
Deliverable: Zip file of entire solution Folder including the planning file.
Recall that an array is a list of information of the same data type. There are various ways to sort data in programming.
Bubble sort is one such technique that sorts numbers in a list by comparing them 2 at a time and swapping if necessary.
Bubble sort can go Ascending (A to Z. least to greatest), or Descending (Z to A greatest to least)
Study bubble sort here: https://visualgo.net/en/sorting?slide=1 (Links to an external site.)
Be sure to add the attached .h to your .cpp file. DO NOT re-write the array…It lives in the header file.
#include “accounts.h”
Also, refer to the “maxAccounts” variable from the file.
Task:
Create a Folder on your saving location called “Unit6_YourLastName_Task_X” where “X” is replaced by the respective task number below.
Background – Use the provided accounts.h header file in your new program. Open accounts.h and note its contents.
#include “accounts.h”
Write a function that will sort the accountBalances array using the bubble sort ASC algorithm.
Display the cpuTime used.
//Notes
//Using the cpuTime()
//start of program
cout << "Start: " << cpuTime() << endl;
//end of program
cout << "End " << cpuTime() << endl; //document your end time for our Results.txt file.
Create a file called Results.txt. You will use this file to log/write cpuTime() information for the four runs of the program below.
Helpful Reference: Passing Arrays to Functions – Dr_T
https://repl.it/@TysonMcMillan/PassingCPPArraysToF… (Links to an external site.)
Task 1 – Write a pseudocode description and a flowchart for the task in "Background" item 2. What is the process for sorting the array with Bubble Sort ASC. Document this step by step process.
1.
1. Unit6_Pseudocode_Program1_Yourlastname
2. Unit6_Flowchart_Program1_Yourlastname
Task 2 – Write Program 1 :Write a program that will do the task defined in your pseudocode:
1.
1. UNIT6_Program1_YourLastName.cpp (or main.cpp)
2. Use Bubble sort to sort the array ASC
3. Print the array output to the screen using: void printArray(int array[], int size);
4. Run the program 3 times
5. Write/type your results in the Results.txt file with the label "Bubble Sort ASC"
Task 3 – Write Program 2 :Using program1 as a guide, sort the results in reverse order.
1.
1. UNIT6_Program2_YourLastName.cpp (or main.cpp)
2. Use Bubble sort to sort the array DESC
3. Print the array output to the screen using: void printArray(int array[], int size);
4. Run the program 3 times
5. Write/type your results in the Results.txt file with the label "Bubble DESC"
Task 4 – Write Program 3 :Using program1, create an additional function for sorting that uses the standard sort function instead of the bubble sort and call this new method for sorting. See http://www.cplusplus.com/articles/NhA0RXSz/ (Links to an external site.)
How does built-in sort work?
#include //to use the sort command
int main() {
int myArray[7] = {1,2,3,4,5,6,7};
sort(myArray,myArray+7); //sort(nameOfArray,nameOfArray+sizeOfArray); //ASC
}
Be reminded that we know the name of or array is accountsBalances and the size of our array is stored in maxAccounts
sort(accountBalances, accountBalances+maxAccounts);
1. UNIT6_Program3_YourLastName.cpp (or main.cpp)
2. Use built-in sort function to sort the array ASC
3. Print the array output to the screen using: void printArray(int array[], int size);
4. Run the program 3 times
5. Write/type your results in the Results.txt file with the label “Sort Built-in ASC”
Task 5 – Write Program 4 :Using program1, create an additional function for sorting that uses the standard sort function instead of the bubble sort and call this new method for sorting.
(Links to an external site.)
How does built-in sort work in reverse (DESC)?
#include //to use the sort command
int main() {
int myArray[7] = {1,2,3,4,5,6,7};
sort(myArray,myArray+7); //sort(nameOfArray,nameOfArray+sizeOfArray); //ASC
reverse(myArray,myArray+7); //call sort first, then reverse the order DESC
}
Be reminded that we know the name of or array is accountBalances and the size of our array is stored in maxAccounts
sort(accountBalances, accountBalances+maxAccounts);
reverse(accountBalances, accountBalances+maxAccounts);
1.
1.
1. UNIT6_Program4_YourLastName.cpp (or main.cpp)
2. Use built-in sort function to sort the array DESC
3. Print the array output to the screen using: void printArray(int array[], int size);
4. Run the program 3 times
5. Write/type your results in the Results.txt file with the label “Sort Built-in DESC”
Task 6 – Finally, in Results.txt write a brief summary about the average speed of each program run. Which program/process ran the fastest on average?
Task 7 – Using zip software, Zip the Entire Unit6_YourLastName folder naming the resulting file Unit6_YourLastName.zip

o How to Compress or Uncompress a file in Windows (Links to an external site.)
Instructions for Submission:

o Upload your UNIT6_YourLastName.zip file
o
In the “written submission/comments” Provide Dr_T the Live URL share of1) Repl.it share URL(4), and remember to include your planning file and the Result.txt file as well. 2) the URL of your GitHub repository If using an IDE other than Repl.it, provide Dr_T with the zip file submission of your code.
Attachments:
accounts.h : https://inst-fs-iad-prod.inscloudgate.net/files/79…

Categories
C++

Avoid plagiarism

Learning Goal: I’m working on a c++ multi-part question and need an explanation and answer to help me learn.
Avoid plagiarism
Answer the same file, under each question, write the answer
Avoid plagiarism
Write the references
The instruction file must be read carefully and it is a separate file
* Avoid plagiarism
Avoid using paraphrasing programs or websites
• Avoid copying from any source
• Avoid old site files for other teachers
• You must provide a correct, unique and new solution without plagiarism
• Sources should be written in APA style

Categories
C++

Learning goal: i’m working on a c++ discussion question and need an explanation and answer to help me learn.

Learning Goal: I’m working on a c++ discussion question and need an explanation and answer to help me learn.
Homework Goal: Practice writing/using C++ classes, splitting up programs into multiple files, and problem-solving
please add comments! 🙂
in addition to the assignment pdf attached, there are lecture slides that may help you

Categories
C++

This homework was adapted from the web-based word game wordle

Learning Goal: I’m working on a c++ question and need an explanation and answer to help me learn.
Introduction:
This homework was adapted from the web-based word game Wordle
A five-letter secret word has to be guessed in six tries. For each guess, feedback is given about whether each letter is 1) correct and in the right place, 2) correct but in the wrong place, or 3) not in the secret word.
Homework Goal: Practice writing/using C++ classes, splitting up programs into multiple files, and problem-solving.
The Wordle program is broken down into two parts:
The main program has the responsibility for creating objects, unit testing and responding to user input.
Wordle—This class encapsulates all the information for a single Wordle puzzle
please add comments! 🙂
in addition to the assignment pdf attached, there are lecture slides that may help you
remember, you are only doing milestone 1!

Categories
C++

Learning goal: i’m working on a c++ multi-part question and need an explanation and answer to help me learn.

Learning Goal: I’m working on a c++ multi-part question and need an explanation and answer to help me learn.
I have a simple flowchart problem need help.

Categories
C++

Lets learn my friends!

Learning Goal: I’m working on a c++ project and need a reference to help me learn.
Lets learn my friends!
1. please explain what is c++?
2. C++ programming language is often used to create programs?
3. Mention any “library” contained in C++, include the use of the “library”!
4. write the syntax to display the text (“Hello Studypool”) using the c++ programming language!

Categories
C++

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);

Learning Goal: I’m working on a c++ question and need an explanation and answer to help me learn.
I need to implement the scaling for the Triangle or resizing the triangle. The triangle is already implemented just add 3 functions below. Code provided.
HINTS: use Inverse Matrix
Implement inside() virtual function:
1. the user must press the mouse inside the the triangle to drag it. while dragging, the mouse can leave the triangle.
2. 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.
3. support rotation of the triangle around it’s own center (and in any position) using the mouse left button and CTRL modifier key.
4. 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
vector 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){
//glm::vec3 mouseDragged = glm::vec3(x, y, 0);
// cout << "INSIDE" << endl;
}
//————————————————————–
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){
}
“`

Categories
C++

Void ofapp::draw(){ tri.draw();

Learning Goal: I’m working on a c++ discussion question and need an explanation and answer to help me learn.
I need help to implement some of the functions given the code below:
1) Complete the scale transformation and test it using various values for scaling in (x, y) dimensions. For fun, you might want to try animating the scale values in ofApp::update(). Make sure to demonstrate this in your video.
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.
3) Support rotation of the triangle around it’s own center (and in any position) using the mouse left button and CTRL modifier key.
4) 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.
Hints
1) Here is a function I wrote that tests if a point (“the test point”) is inside a triangle.
bool TriangleShape::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;
}2) The triangle vertices are stored in object space . In other words, they are stored relative to the local origin of the triangle (it's center). So how do I check if a points is inside the triangle when the triangle is transformed ? Hint: (a) either transform the triangle points individually before testing using inside() is inside OR (a better solution) transform the test point into object space. (hint use the matrix inverse).
CODE
#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
vector 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;
float angle = 0.0;
glm::vec3 pos = glm::vec3(500, 400, 1);
};
#include "ofApp.h"
void TriangleShape::draw() {
float angle = 0.0;
ofSetColor(ofColor::red);
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){
glm::vec3 mouseDragged = glm::vec3(x, y, 0);
cout << "INSIDE" << endl;
}
//————————————————————–
void ofApp::mousePressed(int x, int y, int button){
if (tri.inside(glm::vec3(x, y, 0)))
cout << "INSIDE" << endl;
}
//————————————————————–
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){
}

Categories
C++

Else return false;

Learning Goal: I’m working on a c++ question and need an explanation and answer to help me learn.
I need help to implement some of the functions given the code below:
1) Complete the scale transformation and test it using various values for scaling in (x, y) dimensions. For fun, you might want to try animating the scale values in ofApp::update(). Make sure to demonstrate this in your video.
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.
3) Support rotation of the triangle around it’s own center (and in any position) using the mouse left button and CTRL modifier key.
4) 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.
Hints
1) Here is a function I wrote that tests if a point (“the test point”) is inside a triangle.
bool TriangleShape::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;
}2) The triangle vertices are stored in object space . In other words, they are stored relative to the local origin of the triangle (it's center). So how do I check if a points is inside the triangle when the triangle is transformed ? Hint: (a) either transform the triangle points individually before testing using inside() is inside OR (a better solution) transform the test point into object space. (hint use the matrix inverse).
CODE
#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
vector 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;
float angle = 0.0;
glm::vec3 pos = glm::vec3(500, 400, 1);
};
#include "ofApp.h"
void TriangleShape::draw() {
float angle = 0.0;
ofSetColor(ofColor::red);
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){
glm::vec3 mouseDragged = glm::vec3(x, y, 0);
cout << "INSIDE" << endl;
}
//————————————————————–
void ofApp::mousePressed(int x, int y, int button){
if (tri.inside(glm::vec3(x, y, 0)))
cout << "INSIDE" << endl;
}
//————————————————————–
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){
}