Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7724959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:49:10+00:00 2026-06-01T04:49:10+00:00

I’m currently having an issue storing mouse coordinates into an array in which a

  • 0

I’m currently having an issue storing mouse coordinates into an array in which a pointer of that array is passed to a function which will then use those coordinates to display a polyline on the screen.

Below is my code. Note that I’ve commented on where the problem seems to be but I thought I’d paste most of the code anyway:

struct mousePoint{
    int x, y;
};

struct Node{
    mousePoint *pointer;
    int corner;
    Node *next;
};

Node *Top;
Node *Bottom;


void init(void){ // doesnt need to be shown, but initialises linked list

// Adds the mouse coords array to the top of the linked list
void AddLines(mousePoint Lines[], int corner){
    Node *temp;
    temp = new Node;
    cout << "(AddLines func) array x1: ";
cout << Lines[0].x << endl;
cout << "(AddLines func) array y1: ";
cout << Lines[0].y << endl;
cout << "(AddLines func) array x2: ";
cout << Lines[1].x << endl;
cout << "(AddLines func) array y1: ";
cout << Lines[1].y << endl;
    temp->pointer = Lines; // <--- I believe this is the error
    temp->corner = corner;
    temp->next = NULL;
    if(Top == NULL){
        Top = temp;
    }else{
        temp->next = Top;
        Top = temp;
        if(Bottom == NULL){
            Bottom = Top;
        }
    }
}

// Draws the polyline based on the coords in the array
void DrawLines(mousePoint Lines[], int corner){
    cout << "(DrawLines func) array x1: ";
cout << Lines[0].x << endl;
cout << "(DrawLines func) array y1: ";
cout << Lines[0].y << endl;
cout << "(DrawLines func) array x2: ";
cout << Lines[1].x << endl;
cout << "(DrawLines func) array y1: ";
cout << Lines[1].y << endl;
    glBegin(GL_LINE_STRIP);
        for(int i = 0; i < corner; i++){
            glVertex2i(Lines[i].x, Lines[i].y);
        }
    glEnd();

}

void display(void){
    Node *current;
        current = Top;
                // cycle through all polylines in linked list
        for(; current != NULL; current = current->next){ 
            DrawLines(current->pointer, current->corner);
        }
    glFlush();
}

void mouse(int button, int state, int x, int y)
{
    static mousePoint Lines[100]; // array to store mouse coords
    static int NumCorner = 0; // counter for mouse click
    if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        Lines[NumCorner].x = x;
        Lines[NumCorner].y = 480 - y;
                // draw individual points
        glBegin(GL_POINTS);
        glVertex2i(Lines[NumCorner].x, Lines[NumCorner].y); 
        glEnd();
        NumCorner++;
    }else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN){
        AddLines(Lines, NumCorner); // add mouse coords to linked list
        NumCorner = 0; // reset counter back to 0
    }
}

int main(int argc, char** argv) // doesnt need to be shown

Basically, when I click on the screen with the mouse, a point should be drawn and at the same time, those coords are saved to an array. The user can keep clicking with the left mouse button to draw points while those coords are saved. The lines won’t draw until the right mouse button is pressed in which the array is stored in a linked list. The linked list is there to store all the different polyline shapes. The issue however, is that the pointer is not correctly pointing to the array and the drawlines function is not correctly drawing the lines.

So for example, If clicked on two points on the display (note the cout statements in the code) then right click, a line is drawn using those two points. However, if I click on another point, a line is drawn from the previous coords WITHOUT me pressing the right mouse button.

(AddLines func) array x1: 338
(AddLines func) array y1: 395
(AddLines func) array x2: 325
(AddLines func) array y1: 308
(DrawLines func) array x1: 338
(DrawLines func) array y1: 395
(DrawLines func) array x2: 325
(DrawLines func) array y1: 308
(DrawLines func) array x1: 383
(DrawLines func) array y1: 224
(DrawLines func) array x2: 325
(DrawLines func) array y1: 308

Notice how it draws an extra line without adding it to the pointer?

I’ve tried using memcpy – the lines are drawn perfectly IF less that 5-6 points were used, any more and the application crashes. Hence why I believe it to a pointer issue

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:49:11+00:00Added an answer on June 1, 2026 at 4:49 am

    Ok, I guess I got it.

    You are using a static array Lines[]. When you do “temp->pointer = Lines;” you are pointing to this static array for each of your polygons. Thus, when draw your first polygon, and then when you try to start a second polygon, you are editing lines of the first one.

    Instead of the line that you already found problematic, you can copy each point in a for loop iterating “corner” times.

    instead:

    temp->pointer = Lines; // <--- I believe this is the error
    

    try:

    temp->pointer = new mousePoint[corner];
    for(int a = 0; a < corner; a++)
    {
       temp->pointer[a] = Lines[a];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.