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 7821879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:46:36+00:00 2026-06-02T07:46:36+00:00

i have several circles drawn programmatically on the screen . I then work out

  • 0

i have several circles drawn programmatically on the screen . I then work out the distance between the finger click’s x and y coordinates with each of the circle’s x and y coordinates.

Which ever distance is less than any of the circle’s radius is the circle that was clicked on. Quite simple really. However im finding that ive got a lot of repeated code and i feel that i can clean up the code but im not sure what the best way to do it at the moment.

any help is appreciated.

float diffx = touch.x - bass.pos.x;
float diffy = touch.y - bass.pos.y;
float dist = sqrt(diffx*diffx + diffy*diffy);
if(dist < bass.radius){

    if(recordingInfo.isRecording){
        //do some stuff related to this button unique

    }
    //play some sound
}

diffx = touch.x - treble.pos.x;
diffy = touch.y - treble.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);

if(dist < treble.radius){
    if(recordingInfo.isRecording){
        //do something related to this button
    }
    //play some sound
}

diffx = touch.x - hihat.pos.x;
diffy = touch.y - hihat.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);

if(dist < hihat.radius){
    if(recordingInfo.isRecording){

        //do shayt related to this button

    }
    //play this sound
}

diffx = touch.x - bassTwo.pos.x;
diffy = touch.y - bassTwo.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);

if(dist < bassTwo.radius){
    if(recordingInfo.isRecording){
        //do some crap regarding this indivudal button

    }
    //play another sound
}

diffx = touch.x - kick.pos.x;
diffy = touch.y - kick.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);

if(dist < kick.radius){
    if(recordingInfo.isRecording){
      //do some funky stuff related to this button
    }
    //play some sound
}

diffx = touch.x - snare.pos.x;
diffy = touch.y - snare.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);

if(dist < snare.radius){
    if(recordingInfo.isRecording){
        //
    }
    //play some sound
}

diffx = touch.x - recordButton.pos.x;
diffy = touch.y - recordButton.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);

if(dist < recordButton.radius){
    //and do some funky stuff audio visual styff gere
}    

diffx = touch.x - play.pos.x;
diffy = touch.y - play.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);
     //code execution if this circle button is hit
}    

or is this fine? i place all of this code in the touchDown method

  • 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-02T07:46:37+00:00Added an answer on June 2, 2026 at 7:46 am

    The repeated code is:

    diffx = touch.x - recordButton.pos.x;
    diffy = touch.y - recordButton.pos.y;
    dist = sqrt(diffx*diffx + diffy*diffy);
    

    Any time code appears more than once you should consider putting it in a function:

    float distance(vec touch, vec button_center) {
        float diffx = touch.x - bass.pos.x;
        float diffy = touch.y - bass.pos.y;
        float dist = sqrt(diffx*diffx + diffy*diffy);
        return dist;
    }
    
    if(distance(touch,bass.pos) < bass.radius){
       ...
    }
    if(distance(touch, treble.pos) < treble.radius){
       ...
    }
    if(distance(touch,hihat.pos) < hihat.radius){
       ...
    }
    

    Of course, you’re also repeating that check to see if a button has been hit:

    bool is_hit(Button b,vec touch) {
        return distance(b.pos,touch) < b.radius;
    }
    
    if(is_hit(bass,touch)) {}
    if(is_hit(treble,touch)) {}
    ...
    

    This is a very simple method of handling events and it’s hard to reduce the repetition further without changing the program’s architecture. If you want something a little more flexible you might want to look into how GUI frameworks handle events. The Cocoa documentation on the events might be a good example to look at: Cocoa Event-Handling Guide

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On my web page, I'd like to have a group of several concentric circles
I have several pages to do with several table on each page. I've got
I have an SVG file that contains several elements (like path, circles, rectangles, etc.).
I have added several custom icons to my buttons, and they work fine if
I have several vehicles that send a data to server each minute. The server
I have a board as a canvas with several shapes drawn on it, some
We have several different optimization algorithms that produce a different result for each run.
The situation: I have several lists; in each of these lists, the text inside
I have a menu that spans several divs, I move between divs by clicking
In my processing program, I have made several circles positioned randomly. I used the

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.