473,327 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Re: Interesting math problem

On Mar 19, 2:17 pm, "BJörn Lindqvist" <bjou...@gmail.comwrote:
On Mon, Mar 17, 2008 at 11:57 PM, Arnaud Delobelle

<arno...@googlemail.comwrote:
def make_slope(distance, parts):
step = distance / float(parts)
intstep = int(step)
floatstep = step - intstep
steps = []
acc = 0.0
for i in range(parts):
acc += floatstep
step = intstep
if acc 0.999:
step += 1
acc -= 1.0
steps.append(step)
return steps
OK then, using list comprehensions. It is more succint, is it easier
to read?
def slope(dist, parts):
return [(i+1)*dist/parts - i*dist/parts for i in xrange(parts)]

Congratulations! You Won! Jeff Schwab's recursive approach is also
cool but this is the most interesting abuse of integer division I have
seen. I don't think any of the variants are readable at a first
glance, but with a comment it should be ok.

--
mvh Björn
I really want to revive this discussion. Arnaud's approach is
definetly cool, but it turns out that in real-world situations it
doesn't work as succint as here.

Try to use it to draw a simple non-anitaliased line in a standrad
python array or buffer object. Suppose we have an array of unsigned
bytes called `buf` where each line takes `pitch` bytes. That's what I
got while trying to take advantage of this approach. No advantage at
all. And what about ability to port the code to C for speed?

def draw_line(buf, pitch, x, y, dx, dy):
if dx == dy == 0:
buf[y * pitch + x] = 0
return
xdir, ydir = 1, 1

if dx < 0:
xdir = -1
dx = abs(dx)
if dy < 0:
ydir = -1
dy = abs(dy)

if dy < dx:
steps = ((i+1) * dx / dy - i * dx / dy for i in xrange(dy))
for step in steps:
start = y * pitch + x
if xdir 0:
buf[start : start + step] = array('B', [0] * step)
else:
buf[start - step : start] = array('B', [0] * step)
x += step * xdir
y += ydir
else:
steps = ((i+1) * dy / dx - i * dy / dx for i in xrange(dx))
for step in steps:
start = y * pitch + x
if ydir 0:
for i in range(start, start + pitch * step, pitch):
buf[i] = 0
else:
for i in range(start, start - pitch * step, -pitch):
buf[i] = 0
x += xdir
y += step * ydir

Please, tell me that I'm wrong and it's really possible to draw lines,
do scan-conversion and so on with such a cool succint constructs!

--
Ivan
Jun 27 '08 #1
1 1104
On Apr 13, 5:35*pm, Ivan Illarionov <ivan.illario...@gmail.comwrote:
On Mar 19, 2:17 pm, "BJörn Lindqvist" <bjou...@gmail.comwrote:
On Mon, Mar 17, 2008 at 11:57 PM, Arnaud Delobelle
<arno...@googlemail.comwrote:
*def make_slope(distance, parts):
** * step = distance / float(parts)
** * intstep = int(step)
** * floatstep = step - intstep
** * steps = []
** * acc = 0.0
** * for i in range(parts):
** * * * acc += floatstep
** * * * step = intstep
** * * * if acc 0.999:
** * * * * * step += 1
** * * * * * acc -= 1.0
** * * * steps.append(step)
** * return steps
*OK then, using list comprehensions. *It is more succint, is it easier
*to read?
*def slope(dist, parts):
* * return [(i+1)*dist/parts - i*dist/parts for i in xrange(parts)]
Congratulations! You Won! Jeff Schwab's recursive approach is also
cool but this is the most interesting abuse of integer division I have
seen. I don't think any of the variants are readable at a first
glance, but with a comment it should be ok.
--
mvh Björn

I really want to revive this discussion. Arnaud's approach is
definetly cool, but it turns out that in real-world situations it
doesn't work as succint as here.

Try to use it to draw a simple non-anitaliased line in a standrad
python array or buffer object. Suppose we have an array of unsigned
bytes called `buf` where each line takes `pitch` bytes. That's what I
got while trying to take advantage of this approach. No advantage at
all. And what about ability to port the code to C for speed?

def draw_line(buf, pitch, x, y, dx, dy):
* * if dx == dy == 0:
* * * * buf[y * pitch + x] = 0
* * * * return
* * xdir, ydir = 1, 1

* * if dx < 0:
* * * * xdir = -1
* * * * dx = abs(dx)
* * if dy < 0:
* * * * ydir = -1
* * * * dy = abs(dy)

* * if dy < dx:
* * * * steps = ((i+1) * dx / dy - i * dx / dy for i in xrange(dy))
* * * * for step in steps:
* * * * * * start = y * pitch + x
* * * * * * if xdir 0:
* * * * * * * * buf[start : start + step] = array('B', [0] * step)
* * * * * * else:
* * * * * * * * buf[start - step : start] = array('B', [0] * step)
* * * * * * x += step * xdir
* * * * * * y += ydir
* * else:
* * * * steps = ((i+1) * dy / dx - i * dy / dx for i in xrange(dx))
* * * * for step in steps:
* * * * * * start = y * pitch + x
* * * * * * if ydir 0:
* * * * * * * * for i in range(start, start + pitch * step, pitch):
* * * * * * * * * * buf[i] = 0
* * * * * * else:
* * * * * * * * for i in range(start, start - pitch * step, -pitch):
* * * * * * * * * * buf[i] = 0
* * * * * * x += xdir
* * * * * * y += step * ydir

Please, tell me that I'm wrong and it's really possible to draw lines,
do scan-conversion and so on with such a cool succint constructs!

--
Ivan
I don't think my answer is suitable for drawing a line the way you are
doing it. FWIW, this is how I would go about it (not tested):

def draw_rectangle(buf, pitch, x, y, w, h):
# Make a mask for w and apply it across h lines

def draw_line(buf, pitch, x, y, w, h):
# w and h can't be < 0
if w < h:
limits = ((i, i*h/w) for i in xrange(1, w+1))
else:
limits = ((i*w/h, i) for i in xrange(1, h+1))
dx0, dy0 = 0, 0
for dx, dy in limits:
draw_rectangle(x+dx0, y+dy0, dx-dx0, dy-dy0)
dx0, dy0 = dx, dy

The positive thing is that it is trivial to extend draw_line so that
it accepts a thickness parameter as well.

--
Arnaud

Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Reiner Apke | last post by:
Hello, I have got a very strange problem with the calcualtion of the the square root (Math.Sqrt()). I calculate in a loop a lot of of diameters maxDiameter = Math.Sqrt(maxCrossSection *...
5
by: Ark | last post by:
Hi everyone, Does anyone know if Direct3D overloads System.Math functions? Also is it possible to access the base functions of the overloaded function (in other words restore original of the...
11
by: Sambo | last post by:
I have the following module: ------------------------------- import math def ac_add_a_ph( amp1, ph1, amp2, ph2 ): amp3 = 0.0 ph3 = 0.0 ac1 = ( 0, 0j ) ac2 = ( 0, 0j )
10
by: David Coleman | last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1) In the Command Window I get the following ? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2)...
6
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls...
40
by: nufuhsus | last post by:
Hello all, First let me appologise if this has been answered but I could not find an acurate answer to this interesting problem. If the following is true: C:\Python25\rg.py>python Python...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
5
by: BEES INC | last post by:
I've been awfully busy programming lately. My Django-based side project is coming along well and I hope to have it ready for use in a few weeks. Please don't ask more about it, that's really all I...
0
by: Gary Herron | last post by:
BEES INC wrote: Much simpler this way. This produces the number of whole start and the number of half stars: v = ... calculate the average ... whole = int(v+0.25) half =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.