2d

Point inside 2D axis aligned rectangle, no branches

家住魔仙堡 提交于 2019-12-06 03:26:19
问题 I'm searching for the most optimized method to detect whether a point is inside an axis aligned rectangle. The easiest solution needs 4 branches (if) which is bad for performance. 回答1: Given a segment [x0, x1] , a point x is inside the segment when (x0 - x) * (x1 - x) <= 0 . In two dimensions case, you need to do it twice, so it requires two conditionals. 回答2: Consider BITWISE-ANDing the values of XMin-X, X-XMax, YMin-Y, Y-YMax and use the resulting sign bit. Will work with both ints and

C# XNA 2D trail effect optimization

百般思念 提交于 2019-12-06 03:10:39
Currently as a trail effect in my game I have for every 5 frames a translucent texture copy of a sprite is added to a List<> of trails. The alpha values of these trails is decremented every frame and a draw function iterates through the list and draws each texture. Once they hit 0 alpha they are removed from the List<>. The result is a nice little trail effect behind moving entities. The problem is for about 100+ entities, the frame rate begins to drop drastically. All trail textures come from the same sprite sheet so i dont think it's batching issue. I profiled the code and the CPU intensity

How to resize an image without loading into memory?

眉间皱痕 提交于 2019-12-06 02:36:21
问题 I'd like to check the dimensions of an image, and then size it down if the width exceeds a given threshold. When I load a 6MB JPEG image with ImageIO.read(), the BufferedImage allocates about 45MB of heap space. Is there a way to do a passthrough image resize without loading all the data into memory? I tried passing ImageIO.read() a File object, thinking it would stream the data from disk, but it doesn't help. 回答1: Check out im4java and JMagick. Both use an extremely efficient external tool

A.I. that can navigate a randomly generated 2D city

荒凉一梦 提交于 2019-12-06 02:12:35
问题 I'm writing an iOS game (Using UIView), which has a randomly generated 2D city. I need attacking A.I., that will take an intelligent path to find the player (without colliding with buildings). Can someone point me in the right direction as to what kind of algorithms I would use to achieve this? Edit: I've decided to use A*. I will create a grid on the map, test every grid intersection point, if that point is inside a building, I'll invalidate the point. The attacking A.I. player will then

2D histogram with Python

守給你的承諾、 提交于 2019-12-06 01:13:45
I'm trying to plot a 2D histogram in Python using these code from math import * import pylab as p import matplotlib.pyplot as plt import numpy as np x=part.points[:,0] y=part.points[:,1] z=part.points[:,2] H, xedges, yedges = np.histogram2d(x, y, bins=(128,128)) H.shape, xedges.shape, yedges.shape extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]] plt.imshow(H, extent=extent, interpolation='nearest') plt.colorbar() plt.xlabel("x") plt.ylabel("y") plt.show() Every thing works fine: I have a color bar which represent the counts in each cells. The thing is that I would like to have the log

python 2d array to dict

末鹿安然 提交于 2019-12-06 00:14:36
I want to create a dict from lower triangle of a symmetric matrix represented as 2d array. For examples if the numpy array is; array([[0, 2, 3], [2, 0, 4], [3, 4, 0]]) then I want the dict to look like; {('1', '0'): 2, ('2', '0'): 3, ('2', '1'): 4} There is a similar post for vector; Fastest way to convert a Numpy array into a sparse dictionary? I am relatively new to python so any help/suggestoins appreciated. >>> arr =[[0, 2, 3], [2, 0, 4], [3, 4, 0]] >>> dict(((j,i), arr[i][j]) for i in range(len(arr)) for j in range(len(arr[0])) if i<j) {(2, 0): 3, (1, 0): 2, (2, 1): 4} One way to do it is

2D collision between a moving circle and a fixed line segment

折月煮酒 提交于 2019-12-05 23:42:17
问题 In the context of a game program, I have a moving circle and a fixed line segment. The segment can have an arbitrary size and orientation. I know the radius of the circle: r I know the coordinates of the circle before the move: (xC1, yC1) I know the coordinates of the circle after the move: (xC2, yC2) I know the coordinates of the extremities of the line segment: (xL1, yL1) - (xL2, yL2) I am having difficulties trying to compute: A boolean : If any part of the circle hits the line segment

How to draw 2D pixel-by-pixel in XNA?

我的未来我决定 提交于 2019-12-05 23:09:13
问题 I'm trying to draw on the screen pixel-by-pixel using XNA, but am having problems with resources. I thought the best way would be to have 1 texture that updates every frame, but I'm having trouble updating it. Here's what I've got so far, just as a test: Texture2D canvas; Rectangle tracedSize; UInt32[] pixels; protected override void Initialize() { tracedSize = GraphicsDevice.PresentationParameters.Bounds; canvas = new Texture2D(GraphicsDevice, tracedSize.Width, tracedSize.Height, false,

Operate on slices of 3D image in SimpleITK and create new 3D image

萝らか妹 提交于 2019-12-05 22:33:20
I have a 3D image read in to SimpleITK (using python) from a NIfTI file, take each axial slice, do something with it and re-insert the new 2D slice into a 3D volume with the (hopefully) appropriate dimensions. For example, output = sitk.Image(original.GetSize(), sitk.sitkFloat32) output.CopyInformation(original) for z in numpy.arange(original.GetDepth()): image = original[:,:,z] << Do Something in SimpleITK>> << Produce a new 2D image = newimage >> output[:,:,z] = newimage The final step is throwing an error In [???]: (executing line ??? of "code.py") Traceback (most recent call last): File

2D Java Game. Moving sprite above tiled images

≡放荡痞女 提交于 2019-12-05 21:36:10
-> Short Introduction, you can SKIP this part I'm glad to be able to finally post on this platform, because I for myself have gained so much knowledge through this community; just by reading. So I wanted to say "Hello everybody, and thank you! Actual Content(prologue): Though I'm developing in Objective-C in my company, I am utterly interested in JAVA development. I am quite comfortable with the syntax but have some major troubles with awt/swing/Graphics2D. The Concept: JAVA Application with a 800*600 Frame. On this Frame you can see 16*12 tiles (i.e. gras.jpg or tree.jpg) with a size of 50px²