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

  • Home
  • SEARCH
  • 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 5943765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:26:32+00:00 2026-05-22T16:26:32+00:00

In my experiments with creating a pixel-centered image editor I’ve been trying to draw

  • 0

In my experiments with creating a pixel-centered image editor I’ve been trying to draw a precise grid overlay to help guide users when trying to access certain pixels. However, the grid I draw isn’t very even, especially at smaller sizes. It’s a regular pattern of one slightly larger column for every few normal columns, so I think it’s a rounding issue, but I can’t see it in my code. Here’s my code:

    - (void)drawRect:(NSRect)dirtyRect
{
    context = [[NSGraphicsContext currentContext] graphicsPort];
    CGContextAddRect(context, NSRectToCGRect(self.bounds));
    CGContextSetRGBStrokeColor(context, 1.0f, 0.0f, 0.0f, 1.0f);
    CGContextStrokePath(context);
    CGContextSetInterpolationQuality(context, kCGInterpolationNone);
    CGContextSetShouldAntialias(context, NO);

    if (image)
    {

        NSRect imageRect = NSZeroRect;
        imageRect.size = CGImageGetSize([image CGImage]);
        drawRect = [self bounds];
        NSRect viewRect = drawRect;
        CGFloat aspectRatio = imageRect.size.width / imageRect.size.height;
        if (viewRect.size.width / viewRect.size.height <= aspectRatio)
        {
            drawRect.size.width = viewRect.size.width;
            drawRect.size.height = imageRect.size.height * (viewRect.size.width / imageRect.size.width);
        }
        else
        {
            drawRect.size.height = viewRect.size.height;
            drawRect.size.width = imageRect.size.width * (viewRect.size.height / imageRect.size.height);
        }

        drawRect.origin.x += (viewRect.size.width - drawRect.size.width) / 2.0;
        drawRect.origin.y += (viewRect.size.height - drawRect.size.height) / 2.0;

        CGContextDrawImage(context, drawRect, [image CGImage]);

        if (showPixelGrid)
        {
            //Draw grid by creating start and end points for vertical and horizontal lines.
            //FIXME: Grid is uneven, especially at smaller sizes. 
            CGContextSetStrokeColorWithColor(context, CGColorGetConstantColor(kCGColorBlack));
            CGContextAddRect(context, drawRect);
            CGContextStrokePath(context);
            NSUInteger numXPoints = (NSUInteger)imageRect.size.width * 2;
            NSUInteger numYPoints = (NSUInteger)imageRect.size.height * 2;
            CGPoint xPoints[numXPoints];
            CGPoint yPoints[numYPoints];
            CGPoint startPoint;
            CGPoint endPoint;
            CGFloat widthRatio = drawRect.size.width / imageRect.size.width;
            CGFloat heightRatio = drawRect.size.height / imageRect.size.height;
            startPoint.x  = drawRect.origin.x;
            startPoint.y = drawRect.origin.y;
            endPoint.x = drawRect.origin.x;
            endPoint.y = drawRect.size.height + drawRect.origin.y;
            for (NSUInteger i = 0; i < numXPoints; i += 2)
            {
                startPoint.x += widthRatio;
                endPoint.x += widthRatio;
                xPoints[i] = startPoint;
                xPoints[i + 1] = endPoint;
            }
            startPoint.x  = drawRect.origin.x;
            startPoint.y = drawRect.origin.y;
            endPoint.x = drawRect.size.width + drawRect.origin.x;
            endPoint.y = drawRect.origin.y;
            for (NSUInteger i = 0; i < numYPoints; i += 2)
            {
                startPoint.y += heightRatio;
                endPoint.y += heightRatio;
                yPoints[i] = startPoint;
                yPoints[i + 1] = endPoint;
            }
            CGContextStrokeLineSegments(context, xPoints, numXPoints);
            CGContextStrokeLineSegments(context, yPoints, numYPoints);
        }
    }

}

Any ideas?

  • 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-22T16:26:33+00:00Added an answer on May 22, 2026 at 4:26 pm

    UPDATE: I managed to get your code running with a few tweaks – where did CGImageGetSize() come from? – and I can’t really see the problem, other than columns aren’t all exactly even at extremely small sizes. That’s just how it has to work though. The only way around this is to either fix scaling to be integer multiples of the image size – in other words, get the largest integer multiple of the image size smaller than the view size -or reduce the number of lines drawn on the screen at very small sizes to get rid of this artefact. There’s a reason the pixel grid only becomes visible when you zoom in a long way in most editors. Not to mention that if the grid is still visible at 3-4x resolution you’re making the view just way too busy.


    I couldn’t run the code you provided because there’s a bunch of class ivars in there, but from a cursory glance, I’d say it has something to do with drawing on pixel boundaries. After you round to an integer to get rid of fuzzy AA artefacts (I notice you turned AA off, but ideally you shouldn’t have to do that), you then need to add 0.5 to your origin to get your line drawn in the center of the pixel rather than on the boundary.

    Like this:

    +---X---+---+---+---+---+
    |   |   |   | Y |   |   |
    +---+---+---+---+---+---+
    
    X : CGPoint (1, 1)
    Y : CGPoint (3.5, 0.5)
    

    You want to draw from the center of the pixel, because otherwise your line straddles two pixels.

    In other words, where you’re setting up xPoints and yPoints, make sure to floor() or round() your values, and then add 0.5.

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

Sidebar

Related Questions

In my experiments I haven't been able to find any functional difference between git
i'm fairly new to django and just trying a couple simple experiments to get
i've been running some experiments of my own with Spring Roo and it seems
I have build a grid of div's as playground for some visual experiments. In
I'm trying to create some nice links over my website. I'm creating a search
As an experiment I’ve been toying with an idea of creating an IIS managed
I'm trying to create a PCA model in OpenCV to hold pixel coordinates. As
I am trying to experiment on creating something where you can rate by no.
I've been creating a little experiment in HTML5 Canvas of a car with some
I'm creating an experimental game (which would eventually become a map editor for a

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.