Androidplot: Remove space to the left of chart - androidplot

I am using androidplot-core-0.6.1.jar and I'm modifying the SimpleXYPlotActivity example from the quickstart tutorial. I have removed some items and margin/padding, but what I want to do and can not find a way how, is to remove the space to the left of the chart. I have marked the space I want to remove with red in the image below. Can this be done?
How it looks
Red shows what I want to remove
Code from the activity:
public class MyXYPlotActivity extends Activity {
private XYPlot plot;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.simple_xy_plot_example);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
plot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
Number[] series1Numbers = {1, 8, 5, 2, 7, 4};
XYSeries series1 = new SimpleXYSeries(
Arrays.asList(series1Numbers), // SimpleXYSeries takes a List so turn our array into a List
SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
"Foo"); // Set the display title of the series
LineAndPointFormatter series1Format = new LineAndPointFormatter();
series1Format.setPointLabelFormatter(new PointLabelFormatter());
series1Format.configure(getApplicationContext(),
R.xml.line_point_formatter_with_plf1);
plot.addSeries(series1, series1Format);
plot.setDomainValueFormat(new DecimalFormat("#"));
plot.setRangeValueFormat(new DecimalFormat("#"));
plot.setTitle("Title");
plot.setRangeBoundaries(0,10, BoundaryMode.FIXED);
int length = 30;
plot.setDomainBoundaries(1,length, BoundaryMode.FIXED);
plot.setRangeStep(XYStepMode.INCREMENT_BY_VAL, 1);
plot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, 1);
plot.setMarkupEnabled(true);
plot.setTicksPerRangeLabel(1);
plot.setTicksPerDomainLabel(1);
plot.getGraphWidget().setDomainLabelOrientation(90);
plot.getLayoutManager().remove(plot.getLegendWidget());
plot.getLayoutManager().remove(plot.getRangeLabelWidget());
plot.getLayoutManager().remove(plot.getDomainLabelWidget());
plot.setPlotMargins(0, 0, 0, 0);
plot.setPlotPadding(0, 0, 0, 0);
plot.setBorderStyle(Plot.BorderStyle.SQUARE, null, null);
plot.getRangeLabelWidget().setHeight(0);
plot.getRangeLabelWidget().setWidth(0);
plot.getRangeLabelWidget().setPadding(0, 0, 0, 0);
plot.getRangeLabelWidget().setMargins(0, 0, 0, 0);
plot.getDomainLabelWidget().setHeight(0);
plot.getDomainLabelWidget().setWidth(0);
plot.getDomainLabelWidget().setPadding(0, 0, 0, 0);
plot.getDomainLabelWidget().setMargins(0, 0, 0, 0);
}
}
EDIT: I found the solution:
XYGraphWidget g = plot.getGraphWidget();
g.setRangeLabelWidth(25);
g.setDomainLabelWidth(25);
This above works.

Looks like you want to reduce/remove the graphWidget's margins and/or padding. Try adding these to your code to see if it is having the desired effect...you'll obviously need to use nonzero values suited for your application:
plot.getGraphWidget().setMarginLeft(0);
plot.getGraphWidget().setPaddingLeft(0);
Also take a look at these similar questions:
Have a GraphWidget fill the entire View in AndroidPlot
How do I remove all space around a chart in AndroidPlot?

Related

libgdx draw text via project draws twice?

I try to draw 2D texts for my 3D world objects with libgdx's camera.project function but have a weird problem.
See the pics below:
As you can see in the pictures, all is well in picture 1 but when I turn around 180 degrees the ball's name (codename 1) is be drawing the blank space also (picture 2). I couldn't get what the problem is and after hours of thinking decided to ask here.
Please help me.
My code is:
public static void drawNames(){
TheGame.spriteBatch.begin();
for(int i = 0; i < TheGame.playerMap.size; i++){
Player ply = TheGame.playerMap.getValueAt(i);
if(!ply.isAlive)
continue;
TheGame.tmpVec.set(ply.getPos().x, ply.getPos().y, ply.getPos().z);
TheGame.cam.project(TheGame.tmpVec);
TheGame.fontArialM.draw(TheGame.spriteBatch, ply.name, TheGame.tmpVec.x, TheGame.tmpVec.y, 0, Align.center, false);
}
TheGame.spriteBatch.end();
}
This is because if you project something that is behind the camera it still gets valid screen coordinates from the project method.
Consider the following that prints the screen coordinates of two world coordinates
PerspectiveCamera camera = new PerspectiveCamera(60, 800, 600);
camera.position.set(0, 0, -10);
camera.lookAt(0, 0, 0);
camera.update();
Vector3 temp = new Vector3();
Vector3 p1 = new Vector3(1, 0, 10); // This is in front of the camera, slightly to the right
Vector3 p2 = new Vector3(0, 0, -100); // This is behind of the camera
camera.project(temp.set(p1));
System.out.println("p1 is at screen " + temp);
if (camera.frustum.pointInFrustum(p1))
System.out.println("p1 is visible to the camera");
else
System.out.println("p1 is not visible to the camera");
camera.project(temp.set(p2));
System.out.println("p2 is at screen " + temp);
if (camera.frustum.pointInFrustum(p2))
System.out.println("p2 is visible to the camera");
else
System.out.println("p2 is not visible to the camera");
In your code, before rendering the text you need to check if the ply.getPos() vector is visible to the camera, and only render the text if it is.
if (TheGame.cam.frustum.pointInFrustum(ply.getPos()) {
TheGame.tmpVec.set(ply.getPos().x, ply.getPos().y, ply.getPos().z);
TheGame.cam.project(TheGame.tmpVec);
TheGame.fontArialM.draw(TheGame.spriteBatch, ply.name, TheGame.tmpVec.x, TheGame.tmpVec.y, 0, Align.center, false);
}
Note that there are other ways to cull things behind the camera that might be more efficient for you.

Draw cube vertices with fewest number of steps

What's the fewest number of steps needed to draw all of the cube's vertices, without picking up the pen from the paper?
So far I have reduced it to 16 steps:
0, 0, 0
0, 0, 1
0, 1, 1
1, 1, 1
1, 1, 0
0, 1, 0
0, 0, 0
1, 0, 0
1, 0, 1
0, 0, 1
0, 1, 1
0, 1, 0
1, 1, 0
1, 0, 0
1, 0, 1
1, 1, 1
I presume it can be reduced less than 16 steps as there are only 12 vertices to be drawn
You can view a working example in three.js javascript here:
http://jsfiddle.net/kmturley/5aeucehf/show/
Well I encoded a small brute force solver for this
the best solution is with 16 vertexes
took about 11.6 sec to compute
all is in C++ (visualization by OpenGL)
First the cube representation:
//---------------------------------------------------------------------------
#define a 0.5
double pnt[]=
{
-a,-a,-a, // point 0
-a,-a,+a,
-a,+a,-a,
-a,+a,+a,
+a,-a,-a,
+a,-a,+a,
+a,+a,-a,
+a,+a,+a, // point 7
1e101,1e101,1e101, // end tag
};
#undef a
int lin[]=
{
0,1,
0,2,
0,4,
1,3,
1,5,
2,3,
2,6,
3,7,
4,5,
4,6,
5,7,
6,7,
-1,-1, // end tag
};
// int solution[]={ 0, 1, 3, 1, 5, 4, 0, 2, 3, 7, 5, 4, 6, 2, 6, 7, -1 }; // found polyline solution
//---------------------------------------------------------------------------
void draw_lin(double *pnt,int *lin)
{
glBegin(GL_LINES);
for (int i=0;lin[i]>=0;)
{
glVertex3dv(pnt+(lin[i]*3)); i++;
glVertex3dv(pnt+(lin[i]*3)); i++;
}
glEnd();
}
//---------------------------------------------------------------------------
void draw_pol(double *pnt,int *pol)
{
glBegin(GL_LINE_STRIP);
for (int i=0;pol[i]>=0;i++) glVertex3dv(pnt+(pol[i]*3));
glEnd();
}
//---------------------------------------------------------------------------
Now the solver:
//---------------------------------------------------------------------------
struct _vtx // vertex
{
List<int> i; // connected to (vertexes...)
_vtx(){}; _vtx(_vtx& a){ *this=a; }; ~_vtx(){}; _vtx* operator = (const _vtx *a) { *this=*a; return this; }; /*_vtx* operator = (const _vtx &a) { ...copy... return this; };*/
};
const int _max=16; // know solution size (do not bother to find longer solutions)
int use[_max],uses=0; // temp line usage flag
int pol[_max],pols=0; // temp solution
int sol[_max+2],sols=0; // best found solution
List<_vtx> vtx; // model vertexes + connection info
//---------------------------------------------------------------------------
void _solve(int a)
{
_vtx *v; int i,j,k,l,a0,a1,b0,b1;
// add point to actual polyline
pol[pols]=a; pols++; v=&vtx[a];
// test for solution
for (l=0,i=0;i<uses;i++) use[i]=0;
for (a0=pol[0],a1=pol[1],i=1;i<pols;i++,a0=a1,a1=pol[i])
for (j=0,k=0;k<uses;k++)
{
b0=lin[j]; j++;
b1=lin[j]; j++;
if (!use[k]) if (((a0==b0)&&(a1==b1))||((a0==b1)&&(a1==b0))) { use[k]=1; l++; }
}
if (l==uses) // better solution found
if ((pols<sols)||(sol[0]==-1))
for (sols=0;sols<pols;sols++) sol[sols]=pol[sols];
// recursion only if pol not too big
if (pols+1<sols) for (i=0;i<v->i.num;i++) _solve(v->i.dat[i]);
// back to previous state
pols--; pol[pols]=-1;
}
//---------------------------------------------------------------------------
void solve(double *pnt,int *lin)
{
int i,j,a0,a1;
// init sizes
for (i=0;i<_max;i++) { use[i]=0; pol[i]=-1; sol[i]=-1; }
for(i=0,j=0;pnt[i]<1e100;i+=3,j++); vtx.allocate(j); vtx.num=j;
for(i=0;i<vtx.num;i++) vtx[i].i.num=0;
// init connections
for(uses=0,i=0;lin[i]>=0;uses++)
{
a0=lin[i]; i++;
a1=lin[i]; i++;
vtx[a0].i.add(a1);
vtx[a1].i.add(a0);
}
// start actual solution (does not matter which vertex on cube is first)
pols=0; sols=_max+1; _solve(0);
sol[sols]=-1; if (sol[0]<0) sols=0;
}
//---------------------------------------------------------------------------
Usage:
solve(pnt,lin); // call once to compute the solution
glColor3f(0.2,0.2,0.2); draw_lin(pnt,lin); // draw gray outline
glColor3f(1.0,1.0,1.0); draw_pol(pnt,sol); // overwrite by solution to visually check correctness (Z-buffer must pass also on equal values!!!)
List
is just mine template for dynamic array
List<int> x is equivalent to int x[]
x.add(5) ... adds 5 to the end of list
x.num is the used size of list in entries
x.allocate(100) preallocate list size to 100 entries (to avoid relocations slowdowns)
solve(pnt,lin) algorithm
first prepare vertex data
each vertex vtx[i] corresponds to point i-th point in pnt table
i[] list contains the index of each vertex connected to this vertex
start with vertex 0 (on cube is irrelevant the start point
otherwise there would be for loop through every vertex as start point
_solve(a)
it adds a vertex index to actual solution pol[pols]
then test how many lines is present in actual solution
and if all lines from lin[] are drawn and solution is smaller than already found one
copy it as new solution
after test if actual solution is not too long recursively add next vertex
as one of the vertex that is connected to last vertex used
to limit the number of combinations
at the end sol[sols] hold the solution vertex index list
sols is the number of vertexes used (lines-1)
[Notes]
the code is not very clean but it works (sorry for that)
hope I did not forget to copy something

Have a GraphWidget fill the entire View in AndroidPlot

Is there a way to have the graph of an AndroidPlot XYPlot fill the entire view without padding?
I am specifically talking about the space on the left side that I marked red in the image:
My goal is to overlay the graph over an image and have it flush with the parent views borders. I haved removed all the labels in my subclass of XYPlot, by setting their Paint to null, the space they would take up remains though.
Here is how I set up the plot:
public void setup() {
this.setBackgroundPaint(null);
setPlotMarginLeft(0);
setRangeBoundaries(-2, 2, BoundaryMode.FIXED);
setDomainBoundaries(0, HISTORY_SIZE, BoundaryMode.FIXED);
XYGraphWidget g = getGraphWidget();
g.setDomainLabelPaint(null);
g.setRangeLabelPaint(null);
g.setDomainOriginLabelPaint(null);
g.setRangeOriginLabelPaint(null);
g.setGridBackgroundPaint(null);
g.setGridPaddingLeft(0);
g.setGridPaddingRight(0);
g.setMarginLeft(0);
g.setBackgroundPaint(null);
g.position(-0.5f, XLayoutStyle.RELATIVE_TO_RIGHT,
-0.5f, YLayoutStyle.RELATIVE_TO_BOTTOM,
AnchorPosition.CENTER);
g.setSize(new SizeMetrics(
0, SizeLayoutType.FILL,
0, SizeLayoutType.FILL));
LayoutManager l = getLayoutManager();
l.remove(this.getDomainLabelWidget());
l.remove(this.getRangeLabelWidget());
l.remove(getLegendWidget());
mSeries = new SimpleXYSeries("Azimuth");
mSeries.useImplicitXVals();
addSeries(mSeries, new LineAndPointFormatter(Color.BLACK, null, null, null));
}
Got it, here is the code I am using to set up a Plot without any margins, padding or labels:
XYGraphWidget g = getGraphWidget();
g.setDomainLabelPaint(null);
g.setRangeLabelPaint(null);
g.setDomainOriginLabelPaint(null);
g.setRangeOriginLabelPaint(null);
g.setGridBackgroundPaint(null);
g.setGridPaddingLeft(0);
g.setGridPaddingRight(0);
g.setMarginLeft(0);
g.setBackgroundPaint(null);
g.position(-0.5f, XLayoutStyle.RELATIVE_TO_RIGHT,
-0.5f, YLayoutStyle.RELATIVE_TO_BOTTOM,
AnchorPosition.CENTER);
g.setSize(new SizeMetrics(
0, SizeLayoutType.FILL,
0, SizeLayoutType.FILL));
LayoutManager l = getLayoutManager();
l.remove(this.getDomainLabelWidget());
l.remove(this.getRangeLabelWidget());
l.remove(getLegendWidget());
g.setRangeLabelWidth(0);
g.setDomainLabelWidth(0);
g.setPadding(0, 0, 0, 0);
g.setMargins(0, 0, 0, 0);
g.setGridPadding(0, 0, 0, 0);
setPlotMargins(0, 0, 0, 0);
setPlotPadding(0, 0, 0, 0);

How to Change Background Color on Processing?

I'm still extremely new to processing and I'm just playing around with it right now. I was hoping to find out how to change the background color between two colors, particularly between white and black, when I click my mouse. I found a code online that has the background color change between several different colors but I can't seem to figure out how the bg color can be changed between two colors. I would particularly like what 'col+=' and 'col%=' represent because I can't seem to find it in the processing tutorial. Please help me! Thank you!
Below is the code that I found.
void setup() {
size(600,400);
smooth();
colorMode(HSB);
}
int col = 0;
void draw() {
background(col,255,255);
}
void mousePressed(){
col+=20;
col%=255;
println(col);
}
"x += y" is shorthand for "x = x + y" and, likewise, "x %=y" is shorthand for "x = x % y" (where % is the modulo operator).
I'm going to assume that you wanted to ask is "how do I change the background from one colour to another, and then back again"; there's two basic ways to do this.
1: set up two (or more) reference colours, an extra "current" colour, and then change what 'current' points to, drawing the background off of that:
color c1 = color(255,0,0), c2 = color(0,0,255), current;
void setup() { current = c1; }
void draw() { background(current); }
void mousePressed() { if(current==c1) { current = c2; } else { current = c1; }}
Every time you click, the program checks which of the two colours "current" points to, and then instead points it to the other colour.
2: set up one colour, and apply some operation that is modulo in 1, or 2, or ... steps:
color c = color(255,0,0);
void draw() { background(c); }
void mousePressed() { c = color( red(c), (green(c)+50)%255, blue(c)); }
Every time you click, the colour "c" gets its green component increased by 50, and then modulo-corrected for 255. So it'll cycle through: 0, 50, 100, 150, 200, 250, 300%255=45, 95, 145, 195, 245, 295%255=40, 90, etc.

How to remove the effect of light / shadow on my model in XNA?

I am developing a small game and I would draw a field-ground(land) with a repeated texture. My problem is the rendered result. This gives the impression of seeing everything around my cube looked as if a light shadow.
Is it possible to standardize the light or remove the shadow effect in my drawing function?
Sorry for my bad english..
Here is a screenshot to better understand my problem.
Here my code draw function (instancing model with vertexbuffer)
// Draw Function (instancing model - vertexbuffer)
public void DrawModelHardwareInstancing(Model model,Texture2D texture, Matrix[] modelBones,
Matrix[] instances, Matrix view, Matrix projection)
{
if (instances.Length == 0)
return;
// If we have more instances than room in our vertex buffer, grow it to the neccessary size.
if ((instanceVertexBuffer == null) ||
(instances.Length > instanceVertexBuffer.VertexCount))
{
if (instanceVertexBuffer != null)
instanceVertexBuffer.Dispose();
instanceVertexBuffer = new DynamicVertexBuffer(Game.GraphicsDevice, instanceVertexDeclaration,
instances.Length, BufferUsage.WriteOnly);
}
// Transfer the latest instance transform matrices into the instanceVertexBuffer.
instanceVertexBuffer.SetData(instances, 0, instances.Length, SetDataOptions.Discard);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
// Tell the GPU to read from both the model vertex buffer plus our instanceVertexBuffer.
Game.GraphicsDevice.SetVertexBuffers(
new VertexBufferBinding(meshPart.VertexBuffer, meshPart.VertexOffset, 0),
new VertexBufferBinding(instanceVertexBuffer, 0, 1)
);
Game.GraphicsDevice.Indices = meshPart.IndexBuffer;
// Set up the instance rendering effect.
Effect effect = meshPart.Effect;
//effect.CurrentTechnique = effect.Techniques["HardwareInstancing"];
effect.Parameters["World"].SetValue(modelBones[mesh.ParentBone.Index]);
effect.Parameters["View"].SetValue(view);
effect.Parameters["Projection"].SetValue(projection);
effect.Parameters["Texture"].SetValue(texture);
// Draw all the instance copies in a single call.
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
Game.GraphicsDevice.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0,
meshPart.NumVertices, meshPart.StartIndex,
meshPart.PrimitiveCount, instances.Length);
}
}
}
}
// ### END FUNCTION DrawModelHardwareInstancing
The problem is the cube mesh you are using. The normals are averaged, but I guess you want them to be orthogonal to the faces of the cubes.
You will have to use a total of 24 vertices (4 for each side) instead of 8 vertices. Each corner will have 3 vertices with the same position but different normals, one for each adjacent face:
If the FBX exporter cannot be configured to correctly export the normals simply create your own cube mesh:
var vertices = new VertexPositionNormalTexture[24];
// Initialize the vertices, set position and texture coordinates
// ...
// Set normals
// front face
vertices[0].Normal = new Vector3(1, 0, 0);
vertices[1].Normal = new Vector3(1, 0, 0);
vertices[2].Normal = new Vector3(1, 0, 0);
vertices[3].Normal = new Vector3(1, 0, 0);
// back face
vertices[4].Normal = new Vector3(-1, 0, 0);
vertices[5].Normal = new Vector3(-1, 0, 0);
vertices[6].Normal = new Vector3(-1, 0, 0);
vertices[7].Normal = new Vector3(-1, 0, 0);
// ...
It looks like you've got improperly calculated / no normals.
Look at this example, specifically part 3.
A normal is a vector that describes the direction that light would reflect off that vertex/poly if shined orthogonally to it.
I like this picture to demonstrate The blue lines are the normal direction at each particular point on the curve.
In XNA, you can calculate the normal of a polygon with vertices vert1,vert2,and vert3 like so:
Vector3 dir = Vector3.Cross(vert2 - vert1, vert3 - vert1);
Vector3 norm = Vector3.Normalize(dir);
In a lot of cases this is done automatically by modelling software so the calculation is unnecessary. You probably do need to perform that calculation if you're creating your cubes in code though.

Resources