To write out X and Y from GRID

I’m working on a grid, which has an origin in the middle of the screen. You should be able to print out points with the left mouse button, printing out points works. But in the function I attached, X is printed correctly after my recalculation. But I can’t get Y, it’s around 0.1 away. But if you were to put -0.1 there, the origin (0.00) does not match.

void WindowClass::DrawPoints(SDL_Renderer* renderer)
{
float screenWidth = 800; // Skärmens bredd
float screenHeight = 600; // Skärmens höjd

float centerX = screenWidth / 2;
float centerY = screenHeight / 2;

static size_t lastPrintedIndex = 0;        // Håller reda på indexet för den senaste utskrivna punkten

SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); // Röd färg för punkterna

for (size_t i = 0; i < pointsf.size(); ++i) {
    if (i >= lastPrintedIndex) {
        const auto& point = pointsf[i];
        float adjustedX = centerX + static_cast<int>(point.first * zoomLevel);
        float adjustedY = centerY - static_cast<int>(point.second * zoomLevel);



        if (adjustedX >= 0 && adjustedX <= screenWidth && adjustedY >= 0 && adjustedY <= screenHeight) {
            SDL_Rect rect = { adjustedX - 1, adjustedY - 1, 3, 3 };
            SDL_RenderFillRect(renderer, &rect);
            std::cout << " adjustedX " << adjustedX << " adjustedY " << adjustedY << std::endl;
            std::cout << " Omräknad adjustedX " << (adjustedX / 400) - 1 << " Omräknad adjustedY " << 1 - ((adjustedY / 300) +0.1) << std::endl;
           
            lastPrintedIndex = i + 1; // Uppdatera indexet för den senaste utskrivna punkten
        }
    }
}

/*
for (const auto& point : pointsf) {
    float adjustedX = centerX + static_cast<int>(point.first * zoomLevel);
    float adjustedY = centerY - static_cast<int>(point.second * zoomLevel);

    
    if (adjustedX >= 0 && adjustedX <= screenWidth && adjustedY >= 0 && adjustedY <= screenHeight) {
        SDL_Rect rect = { adjustedX - 1, adjustedY - 1, 3, 3 };
        SDL_RenderFillRect(renderer, &rect);
        //pointDrawnToScreen = true;  // Test från mig. *******************
        std::cout << "adjustedX " << (adjustedX / 400) - 1 << " adjustedY " << (adjustedY / 299) - 1 << std::endl;  // Testing. *********
    }   // Tror detta konverterar till punkternas kordinater. Går säkert och justera med små decimaler.
} */

if (drawLinesToPoints && !pointsf.empty()) {
    // Konvertera pointsf till en vektor av Vec2
    std::vector<Vec2> vecPoints;
    for (const auto& p : pointsf) {
        vecPoints.push_back(Vec2(p.first, p.second));
    }

    // Använd den anpassade convexHull-funktionen med Vec2
    std::vector<Vec2> hull = convexHull(vecPoints);

    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); // Röd färg för linjerna

    for (size_t i = 0; i < hull.size(); ++i) {
        size_t nextIndex = (i + 1) % hull.size();

        // Använd dina Vec2-objekt direkt för att beräkna start- och slutpunkter
        float startX = centerX + (hull[i].x * zoomLevel);
        float startY = centerY - (hull[i].y * zoomLevel);
        float endX = centerX + (hull[nextIndex].x * zoomLevel);
        float endY = centerY - (hull[nextIndex].y * zoomLevel);

        SDL_RenderDrawLine(renderer, static_cast<int>(startX), static_cast<int>(startY), static_cast<int>(endX), static_cast<int>(endY));
    }
}

// Inom din ritningsloop
if (drawLinesToOrigon) {
    DrawLinesToOrigin(renderer);
}

SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // Återställ färgen

}

Hi Nicko! Is it both “adjustedY” and “Omräknad adjustedY” that is wrong or only the latter? I’m not sure I understand the meaning of “Omräknad adjustedY”. Could you perhaps give an example of a point and tell us what output you got and what output you expected.

It is " Omräknad adjustedY " << 1 - ((adjustedY / 300) +0.1) ".
This calculation: 1 - ((adjustedY / 300) +0.1) . It is better whit 1 - (adjustedY / 300). Then origin is 0.0. The + 0.1 was something i tested. Omräknad=Recalculated (Swedish/Svenska).
The numbers are from doing the kode after “1 - (adjustedY / 300)”.
adjustedX and adjustedY is screen coordinates after adjusting for zoom. But no zoom was on for this.

Prompt: adjustedX 400 adjustedY 300 Omräknad adjustedX 0.00 Omräknad adjustedY 0.00,
screen: X 0.00 Y 0.00.

Prompt: adjustedX 440 adjustedY 161 Omräknad adjustedX 0.100 Omräknad adjustedY 0.46333,
screen: X 0.100 Y 0.3475.

Prompt: adjustedX 458 adjustedY 368 Omräknad adjustedX 0.1450 Omräknad adjustedY 0.226667,
screen: X 0.1450 Y -0.1700.

It is the Y from screen I want to get very close too.

I don’t know how you get those screen coordinates, but is the Y screen coordinate 0.3475 mapped into the value 139 when it gets converted into a point? If so, is that really the correct value? If the range of the screen coordinates goes from -1.0 to +1.0 then shouldn’t the value be something like 105? Maybe there is something wrong with the way you convert from screen coordinates to points (if that is what you do).

EDIT: I meant 105 (not 195 like I wrote before)

You may be on the right track. I am attaching the function that shows screen coordinates.

void WindowClass::ShowCoordinates(SDL_Renderer* renderer, int mouseX, int mouseY)
{
int gridWidth = 800; // Replace with the actual width of your grid
int screenWidth = 800; // The width of the screen
int screenHeight = 600; // Screen height

int centerX = screenWidth / 2;
int centerY = screenHeight / 2;

// Check if the mouse pointer is inside the grid
if (mouseX >= 0 && mouseX <= screenWidth && mouseY >= 0 && mouseY <= screenHeight) {
    // Calculate the grid coordinates from the mouse position
    float gridX = (mouseX - centerX) / (gridWidth / 2.0f);
    float gridY = (centerY - mouseY) / (gridWidth / 2.0f);

    // Add the point to your data structure
    //Here I assume that gridX and gridY can be used as indices. If not, they need to be converted.
    int indexX = static_cast<int>(gridX * 100);  // Adjust this scaling to your needs
    int indexY = static_cast<int>(gridY * 100);  // Adjust this scaling to your needs

   
        // Make sure the indexes are within the bounds of your array
        if (indexX >= 0 && indexX < 100 && indexY >= 0 && indexY < 100) {  
            AddPoint2(gridX, gridY, indexX, indexY);
            //std::cout << " indexX: " << indexX << " indexY: " << indexY << " gridX: " << gridX << " gridY: " << gridY << std::endl;               
        }
   
    // Format the coordinates to four decimal places
    std::stringstream formattedCoords;
    formattedCoords << std::fixed << std::setprecision(4) << "X: " << gridX << " Y: " << gridY;
    std::string coords = formattedCoords.str();

    // Define the text color
    SDL_Color textColor = { 0, 0, 0, 255 }; // Svart text

    // Create an SDL_Surface from the text string
    SDL_Surface* textSurface = TTF_RenderText_Solid(font, coords.c_str(), textColor);

    if (!textSurface) {
        std::cerr << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << std::endl;
        return; // Avbryt funktionen om ytan inte kan skapas
    }

    // Create a texture from surface
    SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);

    // Decide where the text should be placed
    int textX = gridWidth + 40;    // 40 pixels to the right of the grid
    int textY = 10;                // 10 pixels from the top

    // Set the size of the render rectangle
    SDL_Rect renderQuad = { textX, textY, textSurface->w, textSurface->h };

    // Render the text to the screen
    SDL_RenderCopy(renderer, textTexture, NULL, &renderQuad);

    // Clean up
    SDL_FreeSurface(textSurface);
    SDL_DestroyTexture(textTexture);
}

// Call DrawCheckbox to draw the checkboxes.
DrawCheckbox(renderer);
DrawCheckbox02(renderer);
DrawCheckbox03(renderer);
DrawCheckbox04(renderer);
DrawCheckbox05(renderer);

}

Side note: To get proper code formatting on this site, select all code in your post and type the keys ctrl+e.

float gridX = (mouseX - centerX) / (gridWidth / 2.0f);
float gridY = (centerY - mouseY) / (gridWidth / 2.0f);
                                    ~~~~~~~~~
                                        ^
                                        |
                                      This looks suspicious
2 Likes

Yes, that’s the error. Must be height. What is called screen height and screen width is actually grid height and grid width. When I did this, the printout is correct.

 float gridX = (mouseX - centerX) / (screenWidth / 2.0f);
        float gridY = (centerY - mouseY) / (screenHeight / 2.0f);

Thanks, I didn’t know that. :grinning:

Thank you, that was very well solved by you. Been testing this on and off for days. :grin: