Is it possible to use indices and normals together? - opengl
I have problems with lighting in OpenGL. When I don't use indices, shadows look as they should:
let cube: [f32; 324] = [
//positions //normals //color
-0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0,
0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0,
0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0,
0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0,
-0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0,
-0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0,
-0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
-0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
-0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
-0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
-0.5, 0.5, -0.5, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
-0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
-0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
-0.5, -0.5, 0.5, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
-0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
-0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0,
0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0,
0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0,
0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0,
-0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0,
-0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0,
-0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
-0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
-0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
];
When I use indices, I don't get the same result:
let cube: [f32; 72] = [
//positions //normals //colors
-0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0,
-0.5, -0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0,
0.5, -0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0,
0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0,
-0.5, 0.5, 0.5, 0.0, -1.0, 0.0, 0.0, 0.0, 1.0,
-0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 0.0, 0.0, 1.0,
0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 0.0, 0.0, 1.0,
0.5, 0.5, 0.5, 0.0, -1.0, 0.0, 0.0, 0.0, 1.0,
];
let cube_indices: [i32; 36] = [
0, 1, 2,
2, 3, 0,
4, 5, 6,
6, 7, 4,
4, 0, 3,
3, 7, 4,
5, 1, 2,
2, 6, 5,
0, 1, 5,
5, 4, 0,
3, 2, 6,
6, 7, 3,
];
My shaders are from the "Learn OpenGL" tutorial:
vertex shader
fragment shader
Yes of course. The vertex coordinate and the normal vector form a tuple with 6 components (x, y, z, nx, ny, nz). There are no vertices where this 6 components are equal and are used on different sides of the cube, thus you cannot share vertices by indices among cube sides. Indices can only be use for "smooth" meshes, where all the vertex attributes are the same for the adjacent primitives (e.g a sphere).
Anyway you can create 4 vertex tuples for each side of the cube:
let cube: [f32; 216] = [
//positions //normals //color
-0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0,
0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0,
0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0,
-0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0,
-0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
-0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
-0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
-0.5, 0.5, -0.5, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
-0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
-0.5, -0.5, 0.5, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
-0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0,
0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0,
0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0,
-0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0,
-0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
-0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
];
And specify 6 indices (2 triangles) for each side of the cube:
let cube_indices: [i32; 36] = [
0, 1, 2, 0, 2, 3,
4, 5, 6, 4, 6, 7,
8, 9, 10, 8, 10, 11,
12, 13, 14, 12, 14, 15,
16, 17, 18, 16, 18, 19,
20, 12, 22, 20, 22, 23,
];
Of course it's possible. But every time you use the same index, you get the same normal. If you don't want the same normal, you must use a different index.
Related
How to change tooltip direction in Chart.js (2.9.4)?
I have a stacked bar chart in Chart.js 2.9.4. On hovering, the tooltip's tip is positioned at the top-center of each bar, but the tooltip itself is rendered to the right and blocks the view of other bars. I'd like to draw the tooltip above the bar. Is there a way to do this? See the JSFiddle here. (Here's the raw code as required by StackOverflow) // Get the past 14 days for the x-axis var allDates = [ moment('2021-04-04T00:00:00'), moment('2021-04-05T00:00:00'), moment('2021-04-06T00:00:00'), moment('2021-04-07T00:00:00'), moment('2021-04-08T00:00:00'), moment('2021-04-09T00:00:00'), moment('2021-04-10T00:00:00'), moment('2021-04-11T00:00:00'), moment('2021-04-12T00:00:00'), moment('2021-04-13T00:00:00'), moment('2021-04-14T00:00:00'), moment('2021-04-15T00:00:00'), moment('2021-04-16T00:00:00'), moment('2021-04-17T00:00:00'), moment('2021-04-18T00:00:00'), ]; // Simulated distribution per day var distributionPerDay = [ [0.13, 0.05, 0.12, 0.07, 0.11, 0.14, 0.22, 0.06, 0. , 0.1 ], [0.1 , 0.08, 0.1 , 0.09, 0.13, 0.12, 0.18, 0.11, 0.03, 0.06], [0.08, 0.1 , 0.1 , 0.09, 0.15, 0.1 , 0.2 , 0.09, 0.03, 0.06], [0.09, 0.09, 0.12, 0.07, 0.13, 0.12, 0.15, 0.14, 0.01, 0.08], [0.05, 0.05, 0.16, 0.13, 0.1 , 0.1 , 0.1 , 0.1 , 0.11, 0.1 ], [0.05, 0.05, 0.16, 0.13, 0.1 , 0.13, 0.1 , 0.12, 0.07, 0.09], [0.08, 0.05, 0.15, 0.1 , 0.13, 0.1 , 0.12, 0.13, 0.08, 0.06], [0.1 , 0.08, 0.13, 0.07, 0.1 , 0.13, 0.15, 0.1 , 0.05, 0.09], [0.08, 0.1 , 0.12, 0.08, 0.13, 0.1 , 0.13, 0.07, 0.07, 0.12], [0.12, 0.08, 0.1 , 0.04, 0.1 , 0.14, 0.11, 0.12, 0.09, 0.1 ], [0.08, 0.06, 0.16, 0.07, 0.07, 0.19, 0.07, 0.14, 0.11, 0.05], [0.08, 0.1 , 0.1 , 0.09, 0.1 , 0.16, 0.12, 0.12, 0.06, 0.07], [0.12, 0.09, 0.14, 0.04, 0.16, 0.08, 0.12, 0.1 , 0.12, 0.03], [0.09, 0.16, 0.1 , 0.07, 0.13, 0.1 , 0.1 , 0.11, 0.08, 0.06], [0.13, 0.12, 0.13, 0.04, 0.15, 0.11, 0.06, 0.12, 0.06, 0.08] ] // Define colors for each bar (10 bars) var colors = ['#9e0142', '#d53e4f', '#f46d43', '#fdae61', '#fee08b', '#e6f598', '#abdda4', '#66c2a5', '#3288bd', '#5e4fa2']; // cb-Spectral // Convert distribution into Chart.js datasets var classNames = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] var datasets = []; classNames.forEach(function(className, i) { // Gather the data {x: date, y: percentage} for this class var data = []; distributionPerDay.forEach(function(dist, j) { data.push({x: allDates[j], y: dist[i]}); }); // Add data and config to the datasets array datasets.push({ label: className, data: data, backgroundColor: colors[i], borderWidth: 0, borderColor: 'black', hoverBorderWidth: 1, // Show a 1px black border on hover borderSkipped: false, // Draw all four borders around the bar minBarLength: 1 // So even a bar for 0 has 1px height }); }); var myChart = new Chart('my-chart', { type: 'bar', data: { datasets: datasets }, options: { hover: { mode: 'dataset', // Highlight an entire row on hover animationDuration: 0 // Otherwise there is flickering when sliding over the bars quickly }, tooltips: { mode: 'nearest', // Only show tooltip data of one bar on hover }, scales: { xAxes: [{ stacked: true, type: 'time', time: { unit: 'day', }, offset: true, // Otherwise the left and right bars are cut off ticks: { source: 'data' } // Use one tick per point in allDates }], yAxes: [{ stacked: true, ticks: { min: 0, max: 1.0 } }] } } });
You can set yAlign: 'bottom' in tooltip options, see the JSFiddle here Relevant part of the code: var myChart = new Chart('my-chart', { type: 'bar', data: { datasets: datasets }, options: { hover: { mode: 'dataset', animationDuration: 0 }, tooltips: { mode: 'nearest', yAlign: 'bottom' //use this to control the placement of the tooltip relative to its point }, scales: { xAxes: [{ stacked: true, type: 'time', time: { unit: 'day', }, offset: true, ticks: { source: 'data' } }], yAxes: [{ stacked: true, ticks: { min: 0, max: 1.0 } }] } } }); It isn't mentioned in the official documentation, but works for Chart.js 2.9.4 (you can have a look at the source code where that option is used to override the automatic alignment calculation based on tooltip and chart size)
chart js (version 2) bar chart superimpose one data set onto another
I have a horizontal bar chart with two datasets I am trying to superimpose the blue bars onto red.so that the intesection can be a start time and the bar can indicate a range. is there any way to do this? data: { labels: ["C#", "ASP.NET MVC", "WebAPI", "SQL", "Entity Framework","NServiceBus / MSMQ", "WCF", "WPF / XAML", "", "HTML / CSS", "JavaScript", "Angular JS v1","", "DI / IoC", "TDD / Unit Testing", "UI Testing (Seleno)", "CI (Teamcity)"], datasets: [ { label:"# years", data: [3, 2, 1, 4, 6, 2, 0.5, 0.25, 0, 7, 2, 0.5, 0, 2, 2, 0.5, 0.5], backgroundColor: 'red', borderWidth: 0 }, { label:"# years", data: [6, 4, 3, 6, 6, 2, 0.5, 0.25, 0, 7, 2, 0.5, 0, 2, 2, 0.5, 0.5], backgroundColor: 'blue', borderWidth: 0 } ] }
options:{ scales: { xAxes: [{ stacked:true}] } }
The integer linear programming(ILP) function in CVXOPT returns non integers
I wanted to optimize a function using ILP implementing by CVXOPT , GLPK in python. I wrote this code, but it gives me non integer solution especially 0.5. Could anyone help me? import math import numpy as np import cvxopt from cvxopt import glpk from cvxopt import matrix G = np.array([ [-1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., 0., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., 0., 1., -1., 0., 1., 1., 0., 0., -1., 0., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., 1., -1., 0., 1., -1., 0., 1., -1., 0., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., 1., 1., 0., 0., 0., 0., 0., -1., 0., 1., -1., 0., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., 1., 1., 0., 0., 0., 0., 0., 0., -1., 1., 1., 0., 0., 0., -1., 1., -1., 0., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., 1., 1., -1., 1., 1., 0., 0., 0., -1., 1., 1., 0., 0., 0., -1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]) h = np.array([[ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 1.], [ 1.], [ 1.], [ 1.], [ 1.], [ 1.], [ 1.], [ 1.], [ 1.], [ 1.], [ 1.], [ 1.], [ 1.], [ 1.], [ 1.], [ 0.], [ 0.], [ 1.], [ 1.], [ 0.], [ 1.], [ 1.], [ 0.], [ 1.], [ 1.], [ 0.], [ 1.], [ 1.], [ 0.], [ 1.], [ 1.], [ 0.], [ 1.], [ 1.], [ 0.], [ 1.], [ 1.], [ 0.], [ 1.], [ 1.], [ 0.], [ 1.], [ 1.], [ 0.], [ 1.], [ 1.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.]]) W = np.array([[-4046.], [-4046.], [-4046.], [-4046.], [-4046.], [ 4027.], [ 4027.], [ 4032.], [ 4036.], [ 4035.], [ 4031.], [ 4037.], [ 4033.], [ 4030.], [ 4028.]]) W, G, h = matrix(W), matrix(G), matrix(h) status, solution = glpk.ilp(W, G.T, h, I=set([0,1])) print solution
Just we need to set all variables to be binaries: status,solution = glpk.ilp(W, G.T, h,B=set(range(len(W))))
How to make a rotating cube move horizontally?
I used C++/OpenGL and drew a rotating cube. Now I want to make it move horizontally while it is rotating. I put a line glTranslatef(0.01, 0, 0). It moves horizontally but does not rotate. Following is my code. Any idea how to fix it so that the cube can move horizontally across while it is rotating? void drawcube(void) { int p[][3] = { { 1, 1, 1 }, { 1, -1, 1 }, { -1, -1, 1 }, { -1, 1, 1 }, { 1, 1, -1 }, { 1, -1, -1 }, { -1, -1, -1 }, { -1, 1, -1 } }; int e[][4] = { { 0, 3, 2, 1 }, { 3, 7, 6, 2 }, { 7, 4, 5, 6 }, { 4, 0, 1, 5 }, { 0, 4, 7, 3 }, { 1, 2, 6, 5 } }; float c[][3] = { { 1.0, 0, 0 }, { 0, 1.0, 0 }, { 1.0, 1.0, 0.0 },{ 0, 0, 1.0 }, { .6, 0, .6 }, { 0, .6, .6 } }; int i; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glTranslatef(0.01, 0, 0); glRotatef(global.angle, 1.0, 1.0, 1.0); for (i = 0; i < 6; ++i) { glColor3fv(c[i]); glBegin(GL_QUADS); glVertex3iv(p[e[i][0]]); glVertex3iv(p[e[i][1]]); glVertex3iv(p[e[i][2]]); glVertex3iv(p[e[i][3]]); glEnd(); } glutSwapBuffers(); }
Clojure sort-by
I'm trying to understand Clojure more and more, i have the following list: {merchant-data {order-id 72144305, unit-price-dollars 110.0, merchant-discount-dollars 20.5, my-discount-dollars 10.2, session-type CONTROL}, my-data {order-id 72144305, unit-price-dollars 110.5, merchant-discount-dollars 10.0, my-discount-dollars 20.2, session-type control}} {merchant-data {order-id 72144777, unit-price-dollars 200.0, merchant-discount-dollars 30.0, my-discount-dollars 0.0, session-type TEST}, my-data {order-id 72144777, unit-price-dollars 200.0, merchant-discount-dollars 0.0, my-discount-dollars 30.0, session-type test}} {merchant-data {order-id 72145239, unit-price-dollars 50.0, merchant-discount-dollars 14.8, my-discount-dollars 2.0, session-type UNMANAGED}, my-data {order-id 72145239, unit-price-dollars 50.0, merchant-discount-dollars 5.0, my-discount-dollars 15.0, session-type unmanaged}} I'm trying to sort this list based on specific field for example (unit-price-dollars): (sort-by :unit-price-dollars sorted-result) for some reason I'm not getting the sorted list, Please help! if this is not the right way, please suggest the right one!
Firstly it always helps if the data structure you are trying to sort is valid Clojure. Secondly you need to specify the full path to the value you want to sort by. user> (def prices [{:merchant-data {:order-id 72144305, :unit-price-dollars 110.0, :merchant-discount-dollars 20.5, :my-discount-dollars 10.2, :session-type "CONTROL"}, :my-data {:order-id 72144305, :unit-price-dollars 110.5, :merchant-discount-dollars 10.0, :my-discount-dollars 20.2, :session-type "control"}} {:merchant-data {:order-id 72144777, :unit-price-dollars 200.0, :merchant-discount-dollars 30.0, :my-discount-dollars 0.0, :session-type "TEST"}, :my-data {:order-id 72144777, :unit-price-dollars 200.0, :merchant-discount-dollars 0.0, :my-discount-dollars 30.0, :session-type "test"}} {:merchant-data {:order-id 72145239, :unit-price-dollars 50.0, :merchant-discount-dollars 14.8, :my-discount-dollars 2.0, :session-type "UNMANAGED"}, :my-data {:order-id 72145239, :unit-price-dollars 50.0, :merchant-discount-dollars 5.0, :my-discount-dollars 15.0, :session-type "unmanaged"}}]) user> (clojure.pprint/pprint (sort-by #(get-in % [:merchant-data :unit-price-dollars]) prices)) ({:my-data {:my-discount-dollars 15.0, :unit-price-dollars 50.0, :session-type "unmanaged", :order-id 72145239, :merchant-discount-dollars 5.0}, :merchant-data {:my-discount-dollars 2.0, :unit-price-dollars 50.0, :session-type "UNMANAGED", :order-id 72145239, :merchant-discount-dollars 14.8}} {:my-data {:my-discount-dollars 20.2, :unit-price-dollars 110.5, :session-type "control", :order-id 72144305, :merchant-discount-dollars 10.0}, :merchant-data {:my-discount-dollars 10.2, :unit-price-dollars 110.0, :session-type "CONTROL", :order-id 72144305, :merchant-discount-dollars 20.5}} {:my-data {:my-discount-dollars 30.0, :unit-price-dollars 200.0, :session-type "test", :order-id 72144777, :merchant-discount-dollars 0.0}, :merchant-data {:my-discount-dollars 0.0, :unit-price-dollars 200.0, :session-type "TEST", :order-id 72144777, :merchant-discount-dollars 30.0}}) nil
The question itself throws up issues. For example the maps above do not contain unit-price-dollars but their two submaps do. Do you want to sort by the merchant-data one, the my-data one or some combination of both? Attempting the simpler case, sorting by one of the values (randomly picked merchant-data), converting all the symbol keys to keywords, and putting the individual maps into a collection gives: (def test-data [{:merchant-data {:order-id 72144305, :unit-price-dollars 110.0, :merchant-discount-dollars 20.5, :my-discount-dollars 10.2, :session-type :CONTROL}, :my-data {:order-id 72144305, :unit-price-dollars 110.5, :merchant-discount-dollars 10.0, :my-discount-dollars 20.2, :session-type :control}} {:merchant-data {:order-id 72144777, :unit-price-dollars 200.0, :merchant-discount-dollars 30.0, :my-discount-dollars 0.0, :session-type :TEST}, :my-data {:order-id 72144777, :unit-price-dollars 200.0, :merchant-discount-dollars 0.0, :my-discount-dollars 30.0, :session-type :test}} {:merchant-data {:order-id 72145239, :unit-price-dollars 50.0, :merchant-discount-dollars 14.8, :my-discount-dollars 2.0, :session-type :UNMANAGED}, :my-data {:order-id 72145239, :unit-price-dollars 50.0, :merchant-discount-dollars 5.0, :my-discount-dollars 15.0, :session-type :unmanaged}}]) (sort-by (comp :merchant-data :unit-price-dollars) test-data) ; => ;({:merchant-data {:order-id 72145239, :unit-price-dollars 50.0, :merchant-discount-dollars 14.8, :my-discount-dollars 2.0, :session-type :UNMANAGED}, :my-data {:order-id 72145239, :unit-price-dollars 50.0, :merchant-discount-dollars 5.0, :my-discount-dollars 15.0, :session-type :unmanaged}} ; {:merchant-data {:order-id 72144305, :unit-price-dollars 110.0, :merchant-discount-dollars 20.5, :my-discount-dollars 10.2, :session-type :CONTROL}, :my-data {:order-id 72144305, :unit-price-dollars 110.5, :merchant-discount-dollars 10.0, :my-discount-dollars 20.2, :session-type :control}} ; {:merchant-data {:order-id 72144777, :unit-price-dollars 200.0, :merchant-discount-dollars 30.0, :my-discount-dollars 0.0, :session-type :TEST}, :my-data {:order-id 72144777, :unit-price-dollars 200.0, :merchant-discount-dollars 0.0, :my-discount-dollars 30.0, :session-type :test}}) If this isn't what you are after, then you're going to have to make your question clearer.