WPF Graphics.FillPolygon not drawing several polygons without Sleep - wpf

The following code draw several triangles only if with Sleep(1), without sleeping it draws only one triangle:
public void Draw(Graphics g)
{
int count = 3;
for (int i = 0; i < count; i++)
{
System.Drawing.Color color = GetColor();
System.Drawing.Point[] points = GetTriangle();
g.FillPolygon(new System.Drawing.SolidBrush(color), points);
//System.Threading.Thread.Sleep(1);
}
}
Where is this code wrong?
Here is the code of routings:
private System.Drawing.Color GetColor()
{
Random rand = new Random((int)DateTime.Now.Ticks);
byte a = (byte)rand.Next(100); a += 155;
byte r = (byte)rand.Next(255);
byte g = (byte)rand.Next(255);
byte b = (byte)rand.Next(255);
return System.Drawing.Color.FromArgb(a, r, g, b);
}
private System.Drawing.Point[] GetTriangle()
{
Random rand = new Random((int)DateTime.Now.Ticks);
int x0 = rand.Next((int)IMAGE_W);
int y0 = rand.Next((int)IMAGE_H);
int x1 = rand.Next((int)IMAGE_W);
int y1 = rand.Next((int)IMAGE_H);
int x2 = rand.Next((int)IMAGE_W);
int y2 = rand.Next((int)IMAGE_H);
System.Drawing.Point x = new System.Drawing.Point(x0, y0);
System.Drawing.Point y = new System.Drawing.Point(x1, y1);
System.Drawing.Point z = new System.Drawing.Point(x2, y2);
System.Drawing.Point[] points = new System.Drawing.Point[] { x, y, z };
return points;
}

Just a guess: GetTriangle() creates a new instance of Random each time.

Related

Im stuck, I would like to create a method to store all the values(volume, area,r,h) into an array in a ascending order. See code I have so far below

I need a method or way to store all values(r,h,area,volume) in a array for the cylinder class below. The array should contain the values in in ascending order.This is the code below to find area and volume.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Double> shapeProperties = new ArrayList<Double>();
double tc, r, h;
System.out.println("Enter # of test cases");
tc = sc.nextInt();
for (int i = 0; i < tc; i++) {
System.out.println("Enter base radius");
r = sc.nextInt();
System.out.println("Enter height");
h = sc.nextInt();
if (tc > 10 || r > 10 || h > 10) {
System.out.println("Please enter a value under 10");
break;
} else {
System.out.println("Radius r= " + r);
System.out.println("Height h= " + h);
System.out.println("Volume = " + getVolume(r, h));
System.out.println("Area = " + getArea(r, h));
ArrayList<Double> properties = sortPropertiesInArray(r, h, getArea(r,h), getVolume(r,h));
}
}
}
static ArrayList<Double> sortPropertiesInArray(double r, double h, double area, double volume) {
ArrayList<Double> shapeProperties = new ArrayList<Double>();
shapeProperties.add(r);
shapeProperties.add(h);
shapeProperties.add(area);
shapeProperties.add(volume);
Collections.sort(shapeProperties);
return shapeProperties;
}
public static double getVolume(double r, double h) {
return Math.PI * r * r * h;
}
public static double getArea(double r, double h) {
return 2 * Math.PI * r * (r * h);
}
}
Something like below should work:
ArrayList<Double> shapeProperties = new ArrayList<Double>();
shapeProperties.add(r);
shapeProperties.add(h);
shapeProperties.add(area);
shapeProperties.add(volume);
Collections.sort(shapeProperties);
If you want to encapsulate this into a function, you could:
ArrayList<Double> sortPropertiesInArray (double r, double h, double area, double volume) {
ArrayList<Double> shapeProperties = new ArrayList<Double>();
shapeProperties.add(r);
shapeProperties.add(h);
shapeProperties.add(area);
shapeProperties.add(volume);
Collections.sort(shapeProperties);
return shapeProperties;
}
and call it as such:
ArrayList<Double> properties = sortPropertiesInArray(r, h, getArea(r,h), getVolume(r,h));
Example output:

Wpf repeating lines with gradient as background

I need to make a background looks like this:
I found the way to do it with <DrawingBrush TileMode="Tile"> but it's not allow to make a gradient, because this generats many repeating squares. Maybe exists any ways to do it dynamicly, not in xaml?
I had to do something very similar and chose to create a grid dynamically in C#, the method was called upon on the Canvas-loaded event.
The "max_value" is the max size in pixels you want the grid to be, I set mine to 3500.
The "ScaleFactor" is the spacing in between the lines/ the size of the squares, I set mine to 15.
The "Canvas" is simply the canvas you want to apply the grid too.
I did have to extract the code and tweak it from a larger program but it should still work, but I don't have an IDE to test it currently.
class GridLine //Declares a grid-line object
{ //Declares a variety of different line lists, used for different parts of the grid.
private List<Line> XGridline { get; set; } = new List<Line>();
private List<Line> YGridline { get; set; } = new List<Line>();
private Line[] Axis { get; set; } = new Line[2];
//Used to draw the grid.
public GridLine DrawGrid(int max_value, int scale_factor, Canvas canvas) //Draws the grid
{
int x = 0;
int y = 0;
while (x < max_value) // Used to draw lines from the center out to the far right.
{
YGridline.Add(new Line() {
Stroke = new SolidColorBrush(Colors.Black),
StrokeThickness = 2,
X1 = x,
Y1 = -max_value,
X2 = x,
Y2 = max_value
});
canvas.Children.Add(YGridline[YGridline.Count - 1]);
x += scale_factor;
}
x = 0;
y = 0;
while (x > -max_value) //used to draw lines from the center to the far left.
{
YGridline.Add(new Line() {StrokeThickness = 2,
Stroke = new SolidColorBrush(Colors.Black),
X1 = x ,
Y1 = -max_value,
X2 = x,
Y2 = max_value
});
canvas.Children.Add(YGridline[YGridline.Count - 1]);
x -= scale_factor;
}
y = 0;
x = 0;
while (y < max_value) //used to draw lines from the center to the bottom
{
XGridline.Add(new Line() {
Stroke = new SolidColorBrush(Colors.Black),
StrokeThickness = 2,
X1 = -max_value,
Y1 = y,
X2 = max_value,
Y2 = y
});
canvas.Children.Add(XGridline[XGridline.Count - 1]);
y += scale_factor;
}
y = 0;
x = 0;
while (y > -max_value) //Used to draw lines from the center to the top.
{
XGridline.Add(new Line() {
Stroke = new SolidColorBrush(Colors.Black),
StrokeThickness = 2,
X1 = -max_value,
Y1 = y,
X2 = max_value,
Y2 = y
});
canvas.Children.Add(XGridline[XGridline.Count - 1]);
y -= scale_factor;
}
//VERTICLE LINE- Y Axis
Axis[0] = (new Line() { Stroke = new SolidColorBrush(Colors.Black), StrokeThickness = 3, X1 = 0, Y1 = -max_value, X2 = 0, Y2 = max_value });
canvas.Children.Add(Axis[0]);
//Horizontal line - X Axis
Axis[1] = (new Line() { Stroke = new SolidColorBrush(Colors.Black), StrokeThickness = 3, X1 = -max_value, Y1 = 0, X2 = max_value, Y2 = 0 });
canvas.Children.Add(Axis[1]);
return this;
}
}
EDIT:
If you'd just like to have a gradient background, why don't you set the rectangles to have no/ a transparent fill then set a gradient background behind them? I would still suggest looking at my code, because depending on what you intend to do with the grid later you may find it easier to generate a grid pragmatically.

User input giving array of answers

I am trying to create a short code, that allows a user to give an input, and display an array depending of the input.
I am trying to display the speed, and acceleration of a falling object. Input is number of intervals, and time between them.
I am also having problems with int and double..
My code so far, I feel I am not getting any further...:
package fall;
import java.util.Scanner;
public class fallSpeed {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Look at momentant(1), or interval (2):");
int alternativ = input.nextInt();
if (alternativ == 1) {
System.out.print("Seconds of free fall:");
int x;
x = input.nextInt();
double a = 9.81;
double d;
double v;
v = x * a;
d = 0.5 * v * x;
if (x < 0) {
System.out.println("Number must be over 0");
}
else{
System.out.println("Distance:" + d + " " + "meter(s)");
System.out.println("Speed after" + " " + x + " " + "sec.:" + v + " " + "m/s");
}
}
if (alternativ == 2) {
System.out.print("Numbers of intervalls:");
int ant = input.nextInt();
System.out.print("Time between intervalls:");
int step = input.nextInt();
double [] a;
//a.length;
double [] v;
double [] d;
double[] obs= new double [ant];
double[] speed;
double[] acc;
//double[] counter;
for (ant=0;ant<=obs.length;ant++)
int [] antall = [obs.length];
//int [] antall = new int [ant];
// array [counter] = (int) ant*step;
//
// fart = obs * a;
// array[acc] = (int) (speed* counter * 0.5);
System.out.print(ant);
//for(int i=0;i<n;i++){//for reading array
// arr[i]=s.nextInt();
}
}
}

JFreeChart XYPlot time series y value that intersects x

I have a time series chart that has data plotted for each day, however not all days have data. In this scenario, how can I determine/calculate the y value for a given date. For example, in this chart, how can I calculate the y coordinate where x is 01-Mar-2016?
I had a look at a similar thread, but I couldn't apply it to the above requirement.
The thread mentioned above was actually what I needed with a few tweaks for TimeSeries. Here is the code:
private static double interpolate(TimeSeries s, long x)
{
List<?> items = s.getItems();
for (int i=0; i<items.size()-1; i++)
{
TimeSeriesDataItem i0 = (TimeSeriesDataItem) items.get(i);
TimeSeriesDataItem i1 = (TimeSeriesDataItem) items.get(i+1);
long x0 = i0.getPeriod().getFirstMillisecond();
double y0 = i0.getValue().doubleValue();
long x1 = i1.getPeriod().getFirstMillisecond();
double y1 = i1.getValue().doubleValue();
if (x >= x0 && x <= x1)
{
double d = x - x0;
double a = d / (x1-x0);
double y = y0 + a * (y1 - y0);
return y;
}
}
// Should never happen
return 0;
}

Drawing like "from-to" in bitmap image

We know the streight line that mspaint can draw into a picture. Since nested loops fill the whole area (x/y) i was wondering whats the way of doing this. Drawing a line from (x0 y0) of the image to desired x/y. Im using this function for finding the x/y pixel of the bmp:
dword find (FILE* fp, dword xp, dword yp)
{
word bpx = (3*8);
dword offset = (2+sizeof(BMP)+sizeof(DIB));
dword w = 500;
dword row = (((bpx * w) * 4) / 32);
dword pixAddress = (offset) + row * yp + ((xp * bpx) / 8);
return pixAddress;
}
And I've tried with many functions for drawing line from 0x0 to xy, their results are close.. but not entirely.
byte color_pattern[] = { 255, 255, 255 };
dword xy_offset[] = {1, 1};
void bmp_lineto(dword endx, dword endy)
{
int dx = endx - xy_offset[0];
int dy = endy - xy_offset[1];
int twody = 2 * dy;
int twodxdy = 2 * (dy - dx);
int dp = twody - dx;
int X, Y, xEnd, yEnd;
FILE* fp = fopen(convert(FILENAME.text), "rb+");
if(xy_offset[0] > endx)
{
X = endx;
Y = endy;
xEnd = xy_offset[0];
}
else
{
X = xy_offset[0];
Y = xy_offset[1];
xEnd = endx;
}
while(X < xEnd)
{
X = X + 1;
if(dp < 0)
{
dp = dp + twody;
} else { Y = Y + 1; dp = dp + twodxdy;
}
fseek(fp, find(fp, X, Y), SEEK_SET);
fwrite(&color_pattern, 1, 3, fp);
}
}
But the result on the bmp from this code is so... uncertain:
bmp_lineto(200, 230); The entire image is x500 : y460
UPDATED. The y coordinate is same as x. Thats the problem
Take a look at the following code - I adapted this from Rosetta Code
#include <stdio.h>
#include <stdlib.h>
#define NX 40
#define NY 20
typedef unsigned char byte;
typedef struct {
int x;
int y;
} point;
typedef struct{
char M[NX][NY];
} bitmap;
void drawLine(point *a, point*b, bitmap *B, FILE* fp, byte *color_pattern) {
int x0 = a->x, y0 = a->y;
int x1 = b->x, y1 = b->y;
int dx = abs(x1-x0), sx = (x0<x1) ? 1 : -1;
int dy = abs(y1-y0), sy = (y0<y1) ? 1 : -1;
int err = (dx>dy ? dx : -dy)/2, e2;
int index;
while(1){
// the next three lines put the pixel right in the file:
index = (y0 * NX + x0)*3;
fseek(fp, index, SEEK_SET);
fwrite(color_pattern, 1, 3, fp);
B->M[x0][y0]=1; // for code testing
if (x0==x1 && y0==y1) break;
e2 = err;
if (e2 >-dx) { err -= dy; x0 += sx; }
if (e2 < dy) { err += dx; y0 += sy; }
}
}
void printLine(bitmap *B){
int ii, jj;
for(ii=0; ii<NY; ii++) {
for(jj=0; jj<NX; jj++) {
printf("%d", (int)B->M[jj][ii]);
}
printf("\n");
}
}
int main(void) {
FILE *fp;
point start = {34,7};
point end = {14, 17};
bitmap B;
byte color[]={255,255,255};
// initialize map to zero. Want to do same with file I suppose
int ii, jj;
for(ii=0; ii<NX; ii++) {
for(jj=0; jj<NY; jj++) {
B.M[ii][jj]=0;
}
}
fp = fopen("mypicture.bmp", "wb");
drawLine(&start, &end, &B, fp, color);
printLine(&B);
fclose(fp);
}
I think it should be easy to adapt it for your situation. Note I have tried to separate / localize variables a little more - that is usually a good idea; there are still many ways to further improve this code (this is a situation where C++ might be a better language...)
Output of the above:
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000001100000
0000000000000000000000000000000110000000
0000000000000000000000000000011000000000
0000000000000000000000000001100000000000
0000000000000000000000000110000000000000
0000000000000000000000011000000000000000
0000000000000000000001100000000000000000
0000000000000000000110000000000000000000
0000000000000000011000000000000000000000
0000000000000001100000000000000000000000
0000000000000010000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
Looks like the "right" line to me... even though it's got the X going in the negative direction. That's the advantage of starting with proven code (in this case, Bresenham's algorithm as implemented on Rosettacode).
You can look into Bresenham's line algorithm. There are extensions to it that handle anti-aliasing too.

Resources