2d

Drawing a dot grid

淺唱寂寞╮ 提交于 2019-12-08 08:04:23
问题 I'm new to graphics programming. I'm trying to create a program that allows you to draw directed graphs. For a start I have managed to draw a set of rectangles (representing the nodes) and have made pan and zoom capabilities by overriding the paint method in Java. This all seems to work reasonably well while there aren't too many nodes. My problem is when it comes to trying to draw a dot grid. I used a simple bit of test code at first that overlayed a dot grid using two nested for loops: int

2D Array Value Assign After Declaration in C++

蹲街弑〆低调 提交于 2019-12-08 07:06:56
问题 I know when we want to assign values to 2D arrays as we declare the array, we do this: int myArray[2][4] = {{1,2,3,4},{5,6,7,8}}; But how should I assign values "after" declaring it? I want to do something like this: int myArray[2][4]; myArray = {{1,2,3,4},{5,6,7,8}}; When I do it, the compiler gives error. Help please. 回答1: If you want to use std::vector then you can do this: #include <vector> int main() { std::vector< std::vector<int> > arrV ; arrV = { {1,2,3,4}, {5,6,7,8} }; } or using std

More elegant way to create a list of 2D points in Python

那年仲夏 提交于 2019-12-08 06:30:06
问题 I need to create a list of 2D points (x,y) in python. This will do it l = [] for x in range (30,50,5): for y in range (1,10,3): l.append((x,y)) So: print l will produce: [(30, 1), (30, 4), (30, 7), (35, 1), (35, 4), (35, 7), (40, 1), (40, 4), (40, 7), (45, 1), (45, 4), (45, 7)] Is there a more elegant way of doing this? 回答1: Use itertools.product: from itertools import product l = list(product(range(30,50,5), range(1,10,3))) It scales better and should be faster than a generator expression,

Determine coordinates for an object at a specific distance and angle from a point

自古美人都是妖i 提交于 2019-12-08 05:31:53
问题 In a 2d space, I have an object of coordinates x1 and y1 and it is facing a specific direction , we'll call it "viewer". At start, the angle that measures the object rotation is 0, so the object starts always facing the same way. The angle is measured by a variable called yrot. let D be the distance from the object determined by sqrt ((x1-x2)^2 + (y1-y2)^2), consider this distance known. Now, knowing the viewer coordinates, the D distance and the yrot angle I want to determine the coordinates

How to achieve Terraria/Starbound 2d lighting?

浪尽此生 提交于 2019-12-08 05:27:10
问题 I am making a 2d game in the perspective of Terraria/Starbound. I want the lighting to look similar to this: Ive tried to get lighting like this by adding a material on all the sprites in my game and then giving them a sprite diffuse shader. Then I made a point light wherever I needed light. There where two problems with this though: 1) Where the light was most intense, it was draining the color of a sprite and made it lighter. 2) I noticed a big FPS drop (And I only had 1 point light!). Is

pygame moving left and right issue

别说谁变了你拦得住时间么 提交于 2019-12-08 04:42:59
问题 I have started making something on pygame but I have encountered an issue when moving left or right. if I quickly change from pressing the right arrow key to pressing the left one and also let go of the right one the block just stops moving. this is my code bg = "sky.jpg" ms = "ms.png" import pygame, sys from pygame.locals import * x,y = 0,0 movex,movey=0,0 pygame.init() screen=pygame.display.set_mode((664,385),0,32) background=pygame.image.load(bg).convert() mouse_c=pygame.image.load(ms)

Memory issues with two dimensional array

喜你入骨 提交于 2019-12-08 04:26:19
问题 Following this nice example I found, I was trying to create a function that dynamically generates a 2D grid (two dimensional array) of int values. It works fairly well the first couple of times you change the values but if crashes after that. I guess the part where memory is freed doesn't work as it should. void testApp::generate2DGrid() { int i, j = 0; // Delete previous 2D array // (happens when previous value for cols and rows is 0) if((numRowsPrev != 0) && (numColumnsPrev != 0)) { for (i

Drawing lines between JLabel in 2d array grid

ぃ、小莉子 提交于 2019-12-08 04:05:11
问题 I'm trying to draw lines between cells which are JLabel but fail to get their coordinates. The lines must follow a path from a startPoint to an endPoint. In my View class, the below code creates the grid: void createGrid(int rows, int cols) { MyMouseListener listener = new MyMouseListener(); setRows(rows); mainPanel.setLayout(new GridLayout(rows, cols, 1, 1)); mainPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); mainPanel.setBackground(Color.DARK_GRAY); grid = new JLabel[rows]

How to run through a 2d array with a single loop?

我的梦境 提交于 2019-12-08 04:00:19
问题 I was wondering if I could write this very thing but with one single loop, instead of two? for (int row = 0; row < matrix.length; row++) { for (int col = 0; col < matrix[0].length; col++) { if ((row + col) % 2 == 0) { System.out.print(matrix[row][col] + ", "); sum += matrix[row][col]; } } System.out.println("with a sum of " + sum); } Actually just ignore the body of the loop.. It's totally irrelevant, I have no idea whatsoever why I included it.. How to somehow combine the two for loops is my

Camera for 2d game

怎甘沉沦 提交于 2019-12-08 03:40:03
问题 I'm currently making an awesome (in my mind) zombie game and I need to know a good way to make a camera. I am using the Slick2d library along with MarteEngine for java. I'm kinda new to java and jumped straight into a library before really getting deep into swing and such so this is probably a lack of graphics knowledge. I read on a tutorial that you can't actually move the window of the game on the map so instead you need to move the map and objects to make it seem like the camera is moving.