パイソン-355
import sys,Image,math as m
i=Image.open(sys.argv[1])
w,h=i.size
a=i.load()
X,Y=map(int,sys.argv[2:])
t=m.atan2
c=lambda i,j:2*(a[i,j][0]>200)-(a[i,j][1]>200)-1
p=c(X,h-1-Y)
V=H=0
for j in range(h):
for i in range(w):q=c(i,h-1-j);y=Y-j;x=X-i;r=x*x+y*y;f=r and 42.*p*q/r;V+=m.cos(t(x,y))*f;H+=m.sin(t(x,y))*f
d=t(V,H)
print(V**2+H**2)**.5,[d,m.pi*2+d][d<0]
未ゴルフ
import sys
import Image
import math as m
X,Y=map(int,sys.argv[2:]) # The X and Y coordinates of the test pixel
i=Image.open(sys.argv[1]) # Open the image
w,h=i.size # Get the width and height of the image
a=i.load() # Get a pixel access object of the image
V=0 # V = vector sum of Vertical Forces
H=0 # H = vector sum of Horizontal Forces
# Function to calculate the charge of the a pixel at x=i, y=j
def c(i,j):
global a
if a[i,j][0]>200: # If Red > 200
if a[i,j][2]>200: # If Green > 200
return 0 # We assume that pixel is White
else:
return 1 # We assume that pixel is Red
return -1 # Else, we asusme that pixel is Black
p=c(X,h-1-Y) # Assign the charge of the test pixel to p
for j in range(h): # For every y value...
for i in range(w): # For every x value...
q=c(i,h-1-j) # Assign the charge of the current pixel to q
y=Y-j # The y distance of the test pixel from the current pixel
x=X-i # The x distance of the test pixel from the current pixel
rSquared=x*x+y*y # The r-squared distance between the 2 pixels
f=rSquared and 42.*p*q/rSquared # If rSquared is > 0, calculate the force.
# Otherwise, the force is zero
V+=m.cos(m.atan2(x,y))*f # Add the Y component of the force to V
H+=m.sin(m.atan2(x,y))*f # Add the X component of the force to H
d=m.atan2(V,H)
print(V**2+H**2)**.5,[d,m.pi*2+d][d<0]
テストを作成するために使用されるいくつかの小さなプログラム:
import Image
width = 100 # Define your own image width
height = 100 # Define your own image height
image = Image.new("RGB", (width, height), "white")
pixels = image.load()
blacks = [(0,0), (1,1)] # Define your own black pixels
reds = [(0,1), (1,0)] # Define your own red pixels
for x,y in blacks:
pixels[x,height-1-y] = (0,0,0)
for x,y in reds:
pixels[x,height-1-y] = (255,0,0)
image.save("y.png") # Save image
サンプルテスト:

Users-MacBook-Air:pngforces User$ python z3.py y.png 0 0
59.3969696197 0.785398163397

Users-MacBook-Air:pngforces User$ python z3.py y.png 50 50
0.0084 3.92699081699

Users-MacBook-Air:pngforces User$ python z3.py y.png 50 50
0.0084 0.785398163397
最初に投稿するだけです...間違いや問題がある場合は、コメントを下にドロップしてください。ただし、他のことで忙しいので、返答にかなりの時間がかかります。