小编Eri*_*ric的帖子

用于寻找两个2D四边形的交点的AC算法?

我有一个quad类型,定义为:

typedef struct __point {
    float x;
    float y;
} point_t;

typedef struct __quad {
    point_t p1;
    point_t p2;
    point_t p3;
    point_t p4;
} quad_t;
Run Code Online (Sandbox Code Playgroud)

如果我在同一平面上有两个这样的四边形,我希望能够计算出这些四边形的交点.例如,如果我们有四A四B,如果B的任何一个点落在A之外,那么该算法应该产生一个带有点的四边形,如下图所示(A为红色,B为紫色):

例

编辑:点的排序并不重要,因为我稍后会使用这些点构建一个将在A中绘制的四边形.

c algorithm geometry computational-geometry

5
推荐指数
1
解决办法
1582
查看次数

Sublime Text的语法高亮显示python中的正则表达式泄漏到周围的代码中

我有一个sublime文本的问题,通常应该是所有编辑器.当我有这样的正则表达式.

listRegex = re.findall(r'[*][[][[].*', testString)
Run Code Online (Sandbox Code Playgroud)

正则表达式之后的所有文本都将被错误地突出显示,因为[[],特别是[没有关闭括号.虽然这个正则表达式的意图是正确的,但编辑器并不知道这一点.

这只是一个我不知道如何处理的烦恼.有人知道怎么修这个东西吗?

python color-scheme sublimetext2

5
推荐指数
1
解决办法
2002
查看次数

替换字符串中的每个第n个字母

我正在编写一个函数来替换字符串中的每个第n个字母

def replaceN(str, n):
   for i in range(len(str)):
     n=str[i]
     newStr=str.replace(n, "*")
     return newStr
Run Code Online (Sandbox Code Playgroud)

但是当我执行它时,只有第一个字母被替换为*.我确定有一个错误,但我看不到它.

python replace

5
推荐指数
2
解决办法
9173
查看次数

TypeError:不支持的操作数类型 - :'unicode'和'unicode',coords

完整代码在这里

HTML代码

<input type="hidden" id="Latitude" name="Latitude" value={{Longitude}} />
<input type="hidden" id="Longitude" name="Longitude" value={{Longitude}} />

document.getElementById("Latitude").value  =  position.coords.latitude;
document.getElementById("Longitude").value =  position.coords.longitude;    
Run Code Online (Sandbox Code Playgroud)

app.py

Latitude = request.form['Latitude']
Longitude = request.form['Longitude']

messages = database.returnMessagesinRange(float(Latitude),float(Longitude))
Run Code Online (Sandbox Code Playgroud)

database.py

def returnMessagesinRange(longitude,latitude):
    allMessages = Messages.find()
    messagesinRange = []
    for current in allMessages:
        if ((current['longitude']-longitude) * (current['longitude']-longitude) + (current['latitude']-latitude)*(current['latitude']-latitude)) <= 1:
            if messagesinRange == None:
                messagesinRange = [current['text']]
            else:
                messagesinRange.append(current['text'])
    return messagesinRange
Run Code Online (Sandbox Code Playgroud)

当这个运行时,我明白了

if ((current['longitude']-longitude) * (current['longitude']-longitude) + (current['latitude']-latitude)*(current['latitude']-latitude)) <= 1:
Run Code Online (Sandbox Code Playgroud)
TypeError: unsupported operand type(s) for -: 'unicode' and …
Run Code Online (Sandbox Code Playgroud)

html python database unicode

5
推荐指数
1
解决办法
5万
查看次数

当我没有打开终端时,为什么我的node.js应用程序偶尔会挂起?

我有一个我通过SSH运行的nodejs应用程序:

$ tmux
$ node server.js
Run Code Online (Sandbox Code Playgroud)

这将在tmux会话中启动我的节点应用程序.

显然,我没有一直打开SSH会话.

我发现的是偶尔我的应用程序可以处于不会为任何页面提供服务的状态.这可能与应用程序本身有关,或者可能只是一个连接不良的SSH会话.

无论哪种方式,只需登录SSH,运行:

$ tmux attach
Run Code Online (Sandbox Code Playgroud)

将焦点放在窗格上会使所有内容再次响应.


我认为node.js的重点在于一切都是非阻塞的 - 那么这里发生了什么?

node.js tmux

5
推荐指数
1
解决办法
612
查看次数

为单个函数声明异常类型是否合理?

说我有这个代码:

def wait_for_x(timeout_at=None):
    while condition_that_could_raise_exceptions
        if timeout_at is not None and time.time() > timeout_at:
            raise SOMEEXCEPTIONHERE

        do_some_stuff()

try:
    foo()
    wait_for_x(timeout_at=time.time() + 10)
    bar()
except SOMEEXCEPTIONHERE:
    # report timeout, move on to something else
Run Code Online (Sandbox Code Playgroud)

如何SOMEEXCEPTIONHERE为函数选择异常类型?为该函数创建唯一的异常类型是否合理,以便不存在condition_that_could_raise_exceptions引发相同异常类型的危险?

wait_for_x.Timeout = type('Timeout', (Exception,), {})
Run Code Online (Sandbox Code Playgroud)

python exception

5
推荐指数
1
解决办法
81
查看次数

如何使用派生类中的属性覆盖基类中的字段?

我有一个使用字段的基类:

class Base(object):
    def __init__(self, member):
        self.member = member
Run Code Online (Sandbox Code Playgroud)

以及想要将其提升为属性并添加一些行为的派生类:

class Derived(Base):
    @property
    def member(self):
        return super(Derived, self).member

    @member.setter
    def member(self, value):
        print "intercepting setter"
        super(Derived, self).member = value
Run Code Online (Sandbox Code Playgroud)

但是,这并没有正确地委托给基类:

>>> d = Derived(0)
intercepting setter

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    d = Derived(0)
  File "<pyshell#3>", line 3, in __init__
    self.member = 2
  File "<pyshell#6>", line 9, in member
    super(Derived, self).member = value
AttributeError: 'super' object has no attribute 'member'
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

python overriding properties

5
推荐指数
1
解决办法
2700
查看次数

如何使用 python 读取 Windows 平板电脑中的加速度计?

我的平板电脑中有一个加速度计,我可以从 javascript 中读取它。

我如何在Python中访问这些数据?是否有一些 ctypes 技巧可以用来调用 Windows 8 传感器 API 函数?

python windows accelerometer

5
推荐指数
1
解决办法
1165
查看次数

将NamedTemporaryFile传递给Windows上的子进程

为python文档tempfile.NamedTemporaryFile说:

名称是否可以用于第二次打开文件,而命名的临时文件仍然是打开的,因此不同平台(它可以在Unix上使用;它不能在Windows NT或更高版本上使用).

我有一个被调用的程序,prog input.txt我无法触及.我想写一个python函数来给它一个字符串.

这里的各种方法并不常用:

from tempfile import NamedTemporaryFile
Run Code Online (Sandbox Code Playgroud)
  1. 如果在Windows上合法,则不明显

    with NamedTemporaryFile() as f:
        f.write(contents)
        subprocess.check_call(['prog', f.name])  # legal on windows?
    
    Run Code Online (Sandbox Code Playgroud)
  2. 可能会过早删除文件

    with NamedTemporaryFile() as f:
        f.write(contents)
        f.close()  # does this delete the file?
        subprocess.check_call(['prog', f.name])
    
    Run Code Online (Sandbox Code Playgroud)
  3. 不能正常清理

    with NamedTemporaryFile(delete=False) as f:
        f.write(contents)  # if this fails, we never clean up!
    try:
        subprocess.check_call(['prog', f.name])
    finally:
        os.unlink(f.name)
    
    Run Code Online (Sandbox Code Playgroud)
  4. 有点难看

    f = NamedTemporaryFile(delete=False)
    try:
        with f:
            f.write(contents)
        subprocess.check_call(['prog', f.name])
    finally:
        os.unlink(f.name)
    
    Run Code Online (Sandbox Code Playgroud)

哪一项是正确的?

python temporary-files

5
推荐指数
1
解决办法
341
查看次数

HTML5 <header>,<section>和<footer>标签的语义究竟是如何工作的?

我有点疑惑,我应该如何使用HTML5 <header>,<section><footer>标签.目前,我无法确定是否像这样使用它们:

<section id="example_section">
    <header>
        <h1>Example Section</h1>
        <p>etc</p>
    </header>

    <p>Content para 1</p>
    <p>Content para 2</p>
    <p>Content para 3</p>

    <footer>
        Wasn't that swell?
    </footer>
</section>
Run Code Online (Sandbox Code Playgroud)

或者像这样:

<header>
    <h1>Example Section</h1>
    <p>etc</p>
</header>

<section id="example_section">
    <p>Content para 1</p>
    <p>Content para 2</p>
    <p>Content para 3</p>
</section>

<footer>
    Wasn't that swell?
</footer>
Run Code Online (Sandbox Code Playgroud)

或妥协,像这样:

<section id="example_section_wrapper">
    <header>
        <h1>Example Section</h1>
        <p>etc</p>
    </header>
    <section id="example_section">
        <p>Content para 1</p>
        <p>Content para 2</p>
        <p>Content para 3</p>
    </section>
    <footer>
        Wasn't that swell?
    </footer>
</section>
Run Code Online (Sandbox Code Playgroud)

我已经包含了ID以显示这些部分的预期含义.我意识到他们是不必要的.哪种方法正确?

html5 semantic-markup semantics

4
推荐指数
1
解决办法
2233
查看次数