我有一个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中绘制的四边形.
我有一个sublime文本的问题,通常应该是所有编辑器.当我有这样的正则表达式.
listRegex = re.findall(r'[*][[][[].*', testString)
Run Code Online (Sandbox Code Playgroud)
正则表达式之后的所有文本都将被错误地突出显示,因为[[],特别是[没有关闭括号.虽然这个正则表达式的意图是正确的,但编辑器并不知道这一点.
这只是一个我不知道如何处理的烦恼.有人知道怎么修这个东西吗?
我正在编写一个函数来替换字符串中的每个第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)
但是当我执行它时,只有第一个字母被替换为*.我确定有一个错误,但我看不到它.
完整代码在这里
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) 我有一个我通过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的重点在于一切都是非阻塞的 - 那么这里发生了什么?
说我有这个代码:
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) 我有一个使用字段的基类:
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)
我该怎么做?
我的平板电脑中有一个加速度计,我可以从 javascript 中读取它。
我如何在Python中访问这些数据?是否有一些 ctypes 技巧可以用来调用 Windows 8 传感器 API 函数?
在为python文档tempfile.NamedTemporaryFile说:
名称是否可以用于第二次打开文件,而命名的临时文件仍然是打开的,因此不同平台(它可以在Unix上使用;它不能在Windows NT或更高版本上使用).
我有一个被调用的程序,prog input.txt我无法触及.我想写一个python函数来给它一个字符串.
这里的各种方法并不常用:
from tempfile import NamedTemporaryFile
Run Code Online (Sandbox Code Playgroud)
如果在Windows上合法,则不明显
with NamedTemporaryFile() as f:
f.write(contents)
subprocess.check_call(['prog', f.name]) # legal on windows?
Run Code Online (Sandbox Code Playgroud)可能会过早删除文件
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)不能正常清理
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)有点难看
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)哪一项是正确的?
我有点疑惑,我应该如何使用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以显示这些部分的预期含义.我意识到他们是不必要的.哪种方法正确?
python ×7
algorithm ×1
c ×1
color-scheme ×1
database ×1
exception ×1
geometry ×1
html ×1
html5 ×1
node.js ×1
overriding ×1
properties ×1
replace ×1
semantics ×1
sublimetext2 ×1
tmux ×1
unicode ×1
windows ×1