mercredi 28 juillet 2021

How to create an if statement that runs if a function is used

currently I am creating a program in c++ that performs an equation if 4 points are given. However, I want to create a loop that stores the coordinates of the points as they are created. To do this, I want to the condition of the an if statement to be if a function is used. I was wondering how I could set this condition, as I am not sure how to indicate the function being used as true. The loop will automatically store the coordinates selected in the function into an array. My code can be found below, with onMouse being the function that I want to be used in the if statement condition. I was also wondering if this loop should be set in the main function or the onMouse function itself. Thanks for the help!

#include <iostream>
#include<opencv2/core/core.hpp> //Mat is defined there
#include<opencv2/imgproc/imgproc.hpp>  //resize an image
#include<opencv2/highgui/highgui.hpp> //input or output: imread(), imshow()

using namespace std;
using namespace cv;

Mat img;

void onMouse(int event, int x, int y, int flags, void* userdata)
{
    if (event == EVENT_LBUTTONDOWN)
    {
        printf("(%d, %d)\n", x, y);

        circle(img, Point(x, y), 1, Scalar(0, 0, 255), 3);
    }
}


int main(int argc, char** argv)
{
    cv::namedWindow("Spline");
    cv::setMouseCallback("Spline", onMouse);


    img.create(600, 800, CV_8UC3);
    img = Scalar(255, 255, 255);


    //Below are just matrices used for testing the spline function

    

    

    
    //Points manually inputted to be used for formula, will be deleted when loop is created


    double h_values[4][4] = { { 2, -2, 1, 1 }, { -3, 3, -2, -1 }, { 0, 0, 1, 0 }, { 1, 0, 0, 0 } };
    //Standard matrix that is used for spline formula, this will stay the same
    Mat Hermite = Mat(4, 4, CV_64FC1, h_values);

    //Points manually inputted, this is the part that will be replaced with a loop (last two rows are computed from a separate 
    //formula, which is why they are not directly on the last two points)
    double point_values[4][2] = { { 200, 350 }, { 220, 400 }, { 600, 300 }, { 390, 300 } };
    Mat Points = Mat(4, 2, CV_64FC1, point_values);

    //This is how you obtain the values for the final spline formula
    Mat Final = Hermite * Points;
    printf("Final matrix:\n");
    cout << Final << endl;
    
    //Just test points used to figure out spline function, used before the ablility to click to make a circle was found

        //Draw 1st point
    circle(img, Point(200, 350), 1, Scalar(0, 0, 255), 3);

    //Draw 2nd point
    circle(img, Point(220, 400), 1, Scalar(0, 0, 255), 3);

    circle(img, Point(400, 450), 1, Scalar(0, 0, 255), 3);

    circle(img, Point(350, 500), 1, Scalar(0, 0, 255), 3);

    //Draw the spline between 1st and 2nd points
    //Use a loop on t [0, 1], for different t values, compute x(t), y(t); then use circle() to draw it
    // x(t) = axt3 + bxt2 + cxt + dx                               
    // y(t) = ayt3 + byt2 + cyt + dy


    double ax = (int)(Final.at<double>(0, 0));
    double ay = (int)(Final.at<double>(0, 1));
    double bx = (int)(Final.at<double>(1, 0));
    double by = (int)(Final.at<double>(1, 1));
    double cx = (int)(Final.at<double>(2, 0));
    double cy = (int)(Final.at<double>(2, 1));
    double dx = (int)(Final.at<double>(3, 0));
    double dy = (int)(Final.at<double>(3, 1));

    printf("ax:\n");
    cout << ax << endl;
    printf("dx:\n");
    cout << dx << endl;


    //Formula used to draw spline spline

    for (double t = 0.0; t <= 1.0; t += 0.001)
    {
        int x = ax * t * t * t + bx * t * t + cx * t + dx;
        int y = ay * t * t * t + by * t * t + cy * t + dy;
        circle(img, Point(x, y), 1, Scalar(0, 0, 0), 1);
    }



    while (1)
    {

        imshow("Spline", img);
        char c = waitKey(1);
        if (c == 27)
            break;

    }


    return 1;


}

Aucun commentaire:

Enregistrer un commentaire