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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:13:21+00:00 2026-05-27T04:13:21+00:00

I have a pixel/location, we’ll call it 4,4 on a graph. I’m attempting to

  • 0

I have a pixel/location, we’ll call it 4,4 on a graph.
I’m attempting to refactor a function that checks to see if pixels around it, meet a certain criteria, in all 8 directions (diagonally, horizontally, and vertically), and if it meets that criteria, take a common action, that has a return value.

For instance:

int weight=0;
if (CheckWeight(new Point(4,4)) == true)
    weight +=100;
if (CheckWeight(new Point(4,5)) == true)
    weight +=10;
if (CheckWeight(new Point(4,3)) == true)
    weight +=10;
if (CheckWeight(new Point(5,5)) == true)
    weight +=10;
if (CheckWeight(new Point(3,3)) == true)
    weight +=10;
if (CheckWeight(new Point(3,4)) == true)
    weight +=10;
if (CheckWeight(new Point(5,4)) == true)
    weight +=10;
if (CheckWeight(new Point(5,3)) == true)
    weight +=10;
if (CheckWeight(new Point(3,5)) == true)
    weight +=10;

Is there a good way to refactor these, so if I have to change certain things here, such as function I’m calling to check the weight, or condition I’m checking against, or weight increment, that I’m not duplicating my efforts 8 times?

I’ve had other programmers I know already suggest to just combine them under one check, which I obviously can’t do, cause it might meet 3 of these checks, and give me a weight of 30 this time, and 5 of these checks and give me a weight of 50 next time.

Edit: This routine will be run on 1920×1080 pixel maps, so several million times; performance could be a real issue involved in the refactoring.

  • 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-05-27T04:13:22+00:00Added an answer on May 27, 2026 at 4:13 am

    A possible approach would be to refactor the common code to a for loop that would iterate through all points near the target one. Sample code follows:

    var target = new Point(4, 4);
    
    int weight = 0;
    if (CheckWeight(target) == true)
        weight += 100;
    
    var points = GetNearestPointsFrom(target);
    
    foreach (var p in points)
    {
        if (CheckWeight(p) == true)
            weight += 10;
    }
    

    You would need to implement the GetNearestPointsFrom method to return the correct points. By going this way you’re also encapsulating the logic for asserting the nearest points in a separate method, which I find more maintainable.


    Update:
    Taking in consideration that you’re limited by performance constraints and you mention that you may need to change the CheckWeight by another function, I would verify if wrapping the if checks in a method that accepts a Predicate<Point> and a weight increment value would not affect performance too much. This way changing the CheckWeight method or the increment is performed only once. Sample code:

    private static void GetWeight(Predicate<Point> predicate, int weightIncrement)
    {
        int weight = 0;
        if (predicate(new Point(4, 4)) == true)
            weight += 100;
        if (predicate(new Point(4, 5)) == true)
            weight += weightIncrement;
        // ... Remaining checks ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array that contains the RGB colour values for each pixel in
I have a PNG image that has an unsupported bitmap graphics context pixel format.
I have a memory buffer corresponding to my screen resolution (1280x800 at 24-bits-per-pixel) that
I have got a sprite that moves based on touch location. I am using
I have made an image processing module that defines a Pixel type as a
I have a pixel and when I load it, it says: a call to
I have the integer pixel I got from getRGB(x,y) , but I don't have
Basically on GunChester my project I have an few pixel wide gap between #login_top
I have created web pages where am using pixel to position various html elements.
I have an image and want to read the pixel values in a particular

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.