Bucket computation, cutting array with lines
$begingroup$
Given an NxN array, drawing a line from the edge's midpoint to the opposite field how can the N buckets be found covering the majority of the line's path?
A visual aid:
Is there a better way to this than computing the linear equation and iteratively advancing in tiny steps to check which bucket we fall on? If not what is the lower limit step size to choose?
algorithms computational-geometry
New contributor
$endgroup$
add a comment |
$begingroup$
Given an NxN array, drawing a line from the edge's midpoint to the opposite field how can the N buckets be found covering the majority of the line's path?
A visual aid:
Is there a better way to this than computing the linear equation and iteratively advancing in tiny steps to check which bucket we fall on? If not what is the lower limit step size to choose?
algorithms computational-geometry
New contributor
$endgroup$
add a comment |
$begingroup$
Given an NxN array, drawing a line from the edge's midpoint to the opposite field how can the N buckets be found covering the majority of the line's path?
A visual aid:
Is there a better way to this than computing the linear equation and iteratively advancing in tiny steps to check which bucket we fall on? If not what is the lower limit step size to choose?
algorithms computational-geometry
New contributor
$endgroup$
Given an NxN array, drawing a line from the edge's midpoint to the opposite field how can the N buckets be found covering the majority of the line's path?
A visual aid:
Is there a better way to this than computing the linear equation and iteratively advancing in tiny steps to check which bucket we fall on? If not what is the lower limit step size to choose?
algorithms computational-geometry
algorithms computational-geometry
New contributor
New contributor
New contributor
asked 3 hours ago
KilianKilian
1162
1162
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
For the considered line you know the start position (xs, ys) and the end (xe, ye). For instance on the N=7 visual you provide, let's take the first upper left line:
xs = 0
ys = 4.5
xe = 7
ye = 2.5
(I use coordinates right, down starting from upper left corner).
Let's take a parameter t of the linear position on the line with t=0 on start and t=1 on end. You know how many verticals or horizontals your line crosses and at which t value. In our exemple:
- 6 verticals crossed at t = v/7 with v=1 to 6 (not counting outlines).
- 2 horizontal crossed at t = 0.25 and t = 0.75.
Now just sort t increasing all these crossings:
(1/7, V), (0.25, H) (2/7, V), (3/7, V), (4/7, V), (5/7, V), (0.75, H), (6/7, V).
They correspond to cell change starting in cell (1, 5) to cell (7, 3). So you can compute the relative length of the line in each cell. For instance:
cell (1, 5) L = 1/7-0 = 1/7
cell (2, 5) L = 0.25-1/7
...
And you know how to select the X pixels now.
$endgroup$
$begingroup$
Thanks. Let me take a closer look at it and I'll accept your answer once I got it translated into code
$endgroup$
– Kilian
1 hour ago
1
$begingroup$
Ingenious but this seems much less efficient than Bresenham.
$endgroup$
– David Richerby
51 mins ago
add a comment |
$begingroup$
You probably want to look at Bresenham's line drawing algorithm. Rather than moving along the line itself, you move along the $x$-coordinates a "pixel" (i.e., array entry) at a time and compute which $y$-value puts the greatest part of the line in the pixel $(x,y)$.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "419"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Kilian is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcs.stackexchange.com%2fquestions%2f102929%2fbucket-computation-cutting-array-with-lines%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
For the considered line you know the start position (xs, ys) and the end (xe, ye). For instance on the N=7 visual you provide, let's take the first upper left line:
xs = 0
ys = 4.5
xe = 7
ye = 2.5
(I use coordinates right, down starting from upper left corner).
Let's take a parameter t of the linear position on the line with t=0 on start and t=1 on end. You know how many verticals or horizontals your line crosses and at which t value. In our exemple:
- 6 verticals crossed at t = v/7 with v=1 to 6 (not counting outlines).
- 2 horizontal crossed at t = 0.25 and t = 0.75.
Now just sort t increasing all these crossings:
(1/7, V), (0.25, H) (2/7, V), (3/7, V), (4/7, V), (5/7, V), (0.75, H), (6/7, V).
They correspond to cell change starting in cell (1, 5) to cell (7, 3). So you can compute the relative length of the line in each cell. For instance:
cell (1, 5) L = 1/7-0 = 1/7
cell (2, 5) L = 0.25-1/7
...
And you know how to select the X pixels now.
$endgroup$
$begingroup$
Thanks. Let me take a closer look at it and I'll accept your answer once I got it translated into code
$endgroup$
– Kilian
1 hour ago
1
$begingroup$
Ingenious but this seems much less efficient than Bresenham.
$endgroup$
– David Richerby
51 mins ago
add a comment |
$begingroup$
For the considered line you know the start position (xs, ys) and the end (xe, ye). For instance on the N=7 visual you provide, let's take the first upper left line:
xs = 0
ys = 4.5
xe = 7
ye = 2.5
(I use coordinates right, down starting from upper left corner).
Let's take a parameter t of the linear position on the line with t=0 on start and t=1 on end. You know how many verticals or horizontals your line crosses and at which t value. In our exemple:
- 6 verticals crossed at t = v/7 with v=1 to 6 (not counting outlines).
- 2 horizontal crossed at t = 0.25 and t = 0.75.
Now just sort t increasing all these crossings:
(1/7, V), (0.25, H) (2/7, V), (3/7, V), (4/7, V), (5/7, V), (0.75, H), (6/7, V).
They correspond to cell change starting in cell (1, 5) to cell (7, 3). So you can compute the relative length of the line in each cell. For instance:
cell (1, 5) L = 1/7-0 = 1/7
cell (2, 5) L = 0.25-1/7
...
And you know how to select the X pixels now.
$endgroup$
$begingroup$
Thanks. Let me take a closer look at it and I'll accept your answer once I got it translated into code
$endgroup$
– Kilian
1 hour ago
1
$begingroup$
Ingenious but this seems much less efficient than Bresenham.
$endgroup$
– David Richerby
51 mins ago
add a comment |
$begingroup$
For the considered line you know the start position (xs, ys) and the end (xe, ye). For instance on the N=7 visual you provide, let's take the first upper left line:
xs = 0
ys = 4.5
xe = 7
ye = 2.5
(I use coordinates right, down starting from upper left corner).
Let's take a parameter t of the linear position on the line with t=0 on start and t=1 on end. You know how many verticals or horizontals your line crosses and at which t value. In our exemple:
- 6 verticals crossed at t = v/7 with v=1 to 6 (not counting outlines).
- 2 horizontal crossed at t = 0.25 and t = 0.75.
Now just sort t increasing all these crossings:
(1/7, V), (0.25, H) (2/7, V), (3/7, V), (4/7, V), (5/7, V), (0.75, H), (6/7, V).
They correspond to cell change starting in cell (1, 5) to cell (7, 3). So you can compute the relative length of the line in each cell. For instance:
cell (1, 5) L = 1/7-0 = 1/7
cell (2, 5) L = 0.25-1/7
...
And you know how to select the X pixels now.
$endgroup$
For the considered line you know the start position (xs, ys) and the end (xe, ye). For instance on the N=7 visual you provide, let's take the first upper left line:
xs = 0
ys = 4.5
xe = 7
ye = 2.5
(I use coordinates right, down starting from upper left corner).
Let's take a parameter t of the linear position on the line with t=0 on start and t=1 on end. You know how many verticals or horizontals your line crosses and at which t value. In our exemple:
- 6 verticals crossed at t = v/7 with v=1 to 6 (not counting outlines).
- 2 horizontal crossed at t = 0.25 and t = 0.75.
Now just sort t increasing all these crossings:
(1/7, V), (0.25, H) (2/7, V), (3/7, V), (4/7, V), (5/7, V), (0.75, H), (6/7, V).
They correspond to cell change starting in cell (1, 5) to cell (7, 3). So you can compute the relative length of the line in each cell. For instance:
cell (1, 5) L = 1/7-0 = 1/7
cell (2, 5) L = 0.25-1/7
...
And you know how to select the X pixels now.
answered 1 hour ago
VinceVince
1844
1844
$begingroup$
Thanks. Let me take a closer look at it and I'll accept your answer once I got it translated into code
$endgroup$
– Kilian
1 hour ago
1
$begingroup$
Ingenious but this seems much less efficient than Bresenham.
$endgroup$
– David Richerby
51 mins ago
add a comment |
$begingroup$
Thanks. Let me take a closer look at it and I'll accept your answer once I got it translated into code
$endgroup$
– Kilian
1 hour ago
1
$begingroup$
Ingenious but this seems much less efficient than Bresenham.
$endgroup$
– David Richerby
51 mins ago
$begingroup$
Thanks. Let me take a closer look at it and I'll accept your answer once I got it translated into code
$endgroup$
– Kilian
1 hour ago
$begingroup$
Thanks. Let me take a closer look at it and I'll accept your answer once I got it translated into code
$endgroup$
– Kilian
1 hour ago
1
1
$begingroup$
Ingenious but this seems much less efficient than Bresenham.
$endgroup$
– David Richerby
51 mins ago
$begingroup$
Ingenious but this seems much less efficient than Bresenham.
$endgroup$
– David Richerby
51 mins ago
add a comment |
$begingroup$
You probably want to look at Bresenham's line drawing algorithm. Rather than moving along the line itself, you move along the $x$-coordinates a "pixel" (i.e., array entry) at a time and compute which $y$-value puts the greatest part of the line in the pixel $(x,y)$.
$endgroup$
add a comment |
$begingroup$
You probably want to look at Bresenham's line drawing algorithm. Rather than moving along the line itself, you move along the $x$-coordinates a "pixel" (i.e., array entry) at a time and compute which $y$-value puts the greatest part of the line in the pixel $(x,y)$.
$endgroup$
add a comment |
$begingroup$
You probably want to look at Bresenham's line drawing algorithm. Rather than moving along the line itself, you move along the $x$-coordinates a "pixel" (i.e., array entry) at a time and compute which $y$-value puts the greatest part of the line in the pixel $(x,y)$.
$endgroup$
You probably want to look at Bresenham's line drawing algorithm. Rather than moving along the line itself, you move along the $x$-coordinates a "pixel" (i.e., array entry) at a time and compute which $y$-value puts the greatest part of the line in the pixel $(x,y)$.
answered 51 mins ago
David RicherbyDavid Richerby
66.4k15101190
66.4k15101190
add a comment |
add a comment |
Kilian is a new contributor. Be nice, and check out our Code of Conduct.
Kilian is a new contributor. Be nice, and check out our Code of Conduct.
Kilian is a new contributor. Be nice, and check out our Code of Conduct.
Kilian is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Computer Science Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcs.stackexchange.com%2fquestions%2f102929%2fbucket-computation-cutting-array-with-lines%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown