why do i need to click multiple times for the code to work? - p5.js

Im making this game where you can collect pokemon and when you run into a pokeball a new page shows up and shows the pokemon you found and you can click anywhere to continue. I always have to click about 10 times for it to actually work
I thought it may be because the page is in the draw function but when I took it out the draw function it didnt work. Also I dont have this problem with the start page and that is in the draw function
I also have a problem that it sometimes falls through the block when I finish collecting a pokemon but that one should be a little easier to figure out.
https://openprocessing.org/sketch/1786189
function preload() {
sprite = loadImage('sprite1.png')
lava = loadImage('lava3.png')
pokeball1 = loadImage('pokeball.png')
pokeball2 = loadImage('pokeball.png')
pokeball3 = loadImage('pokeball.png')
pokeball4 = loadImage('pokeball.png')
pokeball5 = loadImage('pokeball.png')
pichu = loadImage('0172Pichu.png')
shinx = loadImage('shinximage.png')
wooper = loadImage('194.png')
hoppip = loadImage('Hoppip-Pokemon-PNG-Image.png')
rowlet = loadImage('5859604c4f6ae202fedf2854.png')
srowlet = loadImage('ddhnzm5-18daf778-cd90-49bf-bcc3-3cd51b9c7d02.png')
gymBadges = loadImage('gymbadges.webp')
}
function setup() {
createCanvas(700, 700);
background(100);
/*start = createButton('START')
start.size(400,80)
start.position(350,620)*/
background(194, 220, 255)
//frameRate(30)
/*page1 = createButton('X');
page1.position(10,30);
page1.size(60,60)
page1.style('background-color',255 )
page1.style('border-radius', '10px')
page1.style('font-size', '30px')
page1.mousePressed(back)*/
}
let page = 0
let x = 10
//let y = 300
let y =580
function draw() {
let bcolor = [255, 203, 164, 255];
//let bcolor = [0,0,0,255]
let rightColor = get(x + 47, y + 22.5)
let leftColor = get(x - 3, y + 22.5)
//let downColor = get(x + 23, 350)
let downColor = get(x + 23, y + 47)
let upColor = get(x + 23, y + 50)
let shinyOrNo = int(random(4))
//strokeWeight(10)
//point(x-3,y+22.5)
if (page == 0) {
fill(128, 157, 255)
rect(150, 300, 400, 100, 5)
fill(255)
textSize(60)
text('START', 240, 370)
textSize(28)
text('use arrow keys to play', 200, 450)
}
if (page == 1) {
background(224, 209, 255)
fill(266)
ellipse(x, y + 22.5, 5, 5)
stroke(0)
strokeWeight(5)
//sky
fill(210, 235, 249)
rect(0, 0, 700, 700, 10)
//grass
noStroke()
fill(148, 244, 176)
rect(3, 630, 694, 67)
//obstacles
stroke(0)
strokeWeight(2)
fill(255, 203, 164)
//fill(0)
//noStroke()
rect(70, 610, 45, 20, 2)
rect(140, 590, 45, 40, 2)
rect(210, 570, 45, 60, 2)
rect(278, 540, 290, 25, 2)
rect(600, 530, 40, 25, 2)
//rect(610, 500, 40, 20, 2)
rect(500, 470, 60, 25, 2)
rect(430, 410, 50, 30, 2)
rect(70, 370, 113, 25, 2)
rect(200, 370, 60, 25, 2)
rect(280, 370, 100, 25, 2)
rect(15, 330, 30, 25, 2)
rect(100, 290, 30, 25, 2)
rect(15, 250, 30, 25, 2)
rect(100, 210, 30, 25, 2)
rect(175, 165, 30, 25, 2)
rect(260, 130, 270, 25, 2)
rect(560, 100, 100, 25, 2)
fill(134, 89, 248)
triangle(210, 360, 200, 370, 220, 370)
triangle(230, 360, 220, 370, 240, 370)
triangle(250, 360, 240, 370, 260, 370)
image(lava, 310, 488, 70, 70)
image(pokeball1, 222, 540, 23, 23)
image(pokeball2, 520, 441, 23, 23)
image(pokeball3, 150, 340, 23, 23)
image(pokeball4, 18, 220, 23, 23)
image(pokeball5, 420, 100, 23, 23)
rect(620, 50, 30, 16)
line(650, 66, 650, 99)
//sprite
//print (y)
if (eqColor(downColor, bcolor)) {
} else {
y = y +2
}
//y=y+2
image(sprite, x, y, 45, 45)
if (keyIsPressed) {
if (keyIsDown(RIGHT_ARROW)) {
if (eqColor(rightColor, bcolor)) {
} else {
x = x + 2
}
} else if (keyIsDown(LEFT_ARROW)) {
if (eqColor(leftColor, bcolor)) {
} else {
x = x - 2
}
}
if (keyIsDown(UP_ARROW)) {
if(eqColor(upColor,bcolor)) {
}else{
y=y-6
}
}
} //end of keyispressed
//jump
//if (y = y - 6) {
//y = y + 9
//}
//ground
if (y > 590) {
y = 591
}
//walk off screen
if (x < 0 || x > 700) {
x = 0
} else if (x > 655) {
x = 655
} else if (y < 0) {
y = 0
}
//collisons on the top
/*if (x>60 && x<90) {
y = 565
}*/
//pokeball 1
if (x > 189 && x < 218 && y > 512 && y < 535) {
page = 2
}
if (page == 2) {
fill(192, 176, 232)
rect(0, 0, 700, 700)
image(pichu, 230, 200, 300, 300)
fill(0)
strokeWeight(1)
text('click anywhere to continue', 200, 600)
text('You found Pichu!!', 250, 150)
pokeball1 =loadImage('640px-HD_transparent_picture.png')
}
//print(x,y)
//pokeball 2
if (x > 505 && x < 533 && y > 420 && y < 444) {
page = 3
}
if (page == 3) {
fill(192, 176, 232)
rect(0, 0, 700, 700)
image(shinx, 150, 200, 450, 300)
fill(0)
strokeWeight(1)
text('You found Shinx!!', 250, 150)
textSize(19)
text('fun fact: shinx is micaiah\'s favorite pokemon', 180,530)
textSize(28)
text('click anywhere to continue', 200, 600)
pokeball2 =loadImage('640px-HD_transparent_picture.png')
}
//pokeball 3
if (x > 114 && x < 145 && y > 302 && y < 350) {
page = 4
}
if (page == 4) {
fill(192, 176, 232)
rect(0, 0, 700, 700)
image(wooper, 230, 200, 300, 300)
fill(0)
strokeWeight(1)
text('click anywhere to continue', 200, 600)
text('You found Wooper!!', 250, 150)
pokeball3 =loadImage('640px-HD_transparent_picture.png')
}
//pokeball 4
if (x > 0 && x < 25 && y > 200 && y < 225 && shinyOrNo == 1) {
page = 5
}
if (x > 0 && x < 25 && y > 200 && y < 225 && shinyOrNo == 2) {
page = 5
}
if (page == 5) {
fill(192, 176, 232)
rect(0, 0, 700, 700)
image(rowlet,230,200,300,300)
fill(0)
strokeWeight(1)
text('click anywhere to continue', 200, 600)
text('You found rowlet!!', 250,150)
pokeball4 =loadImage('640px-HD_transparent_picture.png')
}
if (x > 0 && x < 25 && y > 200 && y < 225 && shinyOrNo == 3) {
page = 7
}
if (page == 7) {
fill(192, 176, 232)
rect(0, 0, 700, 700)
image(srowlet,230,200,300,300)
fill(0)
strokeWeight(1)
text('click anywhere to continue', 200, 600)
text('You found a shiny rowlet!!', 250,150)
pokeball4 =loadImage('640px-HD_transparent_picture.png')
}
//print(page)
//pokeball 5
if (x > 382 && x < 415 && y > 84 && y < 115) {
page = 6
}
if (page == 6) {
fill(192, 176, 232)
rect(0, 0, 700, 700)
image(hoppip, 230, 200, 300, 300)
fill(0)
strokeWeight(1)
text('click anywhere to continue', 200, 600)
text('You found Hoppip!!', 250, 150)
pokeball5 =loadImage('640px-HD_transparent_picture.png')
}
//lava
/*if (x > 285 && x < 355 && y > 454 ) {
x = 10
y = 580
}*/
point(322,502)
//flag
if (x > 595 && x < 620 && y < 63) {
page = 8
}
if (page == 8) {
fill(192, 176, 232)
rect(0, 0, 700, 700)
fill(0)
strokeWeight(1)
text('your prize is all of the pokemon gym badges',100,200)
image(gymBadges, 160,200,430,300)
text('You finished the game!!', 220, 150)
}
//spikes
if (x > 200 && x < 250 && y > 323 && y<375) {
x = 10
y = 580
}
/*if(y>330 && y<335){
y=331
}*/
} //end of page 1
//print(x,y)
} //end of draw
function mousePressed() {
if (page ==0 && mouseX >= 150 && mouseX <= 550 && mouseY >= 300 && mouseY <= 400) {
page = 1
}
if (page>1) {
page = 1
print(true)
}
}
function eqColor(a, b) {
return a[0] == b[0] && a[1] == b[1] &&
a[2] == b[2] && a[3] == b[3];
}

Related

Pixels eraser in Processing

I am new using Processing and this time I am trying to do like a paint program. As I chose to draw a grid in the background, I don't know how to make a eraser. If someone has any ideas....
This is my code:
int x = 20, y = 40, w = 70, h = 50;
void setup() {
size(600, 600);
background(255);
stroke(240);
for (int linex = 0; linex < width; linex = linex+40) {
for (int liney = 0; liney < height; liney = liney + 40) {
line(0, liney, width, liney);
}
line(linex, 0, linex, height);
}
fill(255, 0, 0);
rect(x, height - 250, w, h);
fill(0, 255, 0);
rect(x, height - 190, w, h);
fill(0, 0, 255);
rect(x, height - 130, w, h);
fill(0);
rect(x, y, w, h);
textSize(17);
text("Erase everything", 10, 30);
}
void draw() {
if ((keyPressed && key == 'r') || (mousePressed && mouseX > x && mouseX < x + w && mouseY > (height - 250) && mouseY < (height - 250) + h)) {
fill(255, 0, 0);
}
if (keyPressed && key == 'g'|| (mousePressed && mouseX > x && mouseX < x + w && mouseY > (height - 190) && mouseY < (height - 190) + h)) {
fill(0, 255, 0);
}
if (keyPressed && key == 'b' || (mousePressed && mouseX > x && mouseX < x + w && mouseY > (height - 130) && mouseY < (height - 130) + h)) {
fill(0, 0, 255);
}
if (keyPressed && key == 'n'){
fill(0);
}
if (mousePressed) {
noStroke();
ellipse(mouseX, mouseY, 20, 20);
}
if (mouseX > x && mouseX < x + w && mouseY > y && mouseY < y+h && mousePressed) {
setup();
}
}
Thanks in advance
Just sneak the following code somewhere into the draw() function in your program.
noStroke();
ellipse(mouseX, mouseY, 8, 8); // change width and height to the diameter (in pixels) you want your eraser to have.
strokeWeight(1);
Or, if you want a square instead of a circle:
noStroke();
rectMode(CENTER);
rect(mouseX, mouseY, 8, 8);
rectMode(CORNER);
strokeWeight(1);
Or something in beetween:
noStroke();
rectMode(CENTER);
rect(mouseX, mouseY, 8, 8, 4); // the 5th parameter is the edge smoothness (in pixels)
rectMode(CORNER);
strokeWeight(1);

Dual pivot quick sort algorithm

I was analyzing the code for Arrays.sort() method in java . My question is for what values of integer array a[] will this code return true ?
if (less < e1 && e5 < great)
After Sorting left and right parts recursively, excluding known pivots for what value of array a[] will the center part become too large (comprises > 4/7 of the array) ?
Given QUICKSORT_THRESHOLD = 286 .
Array size cannot be more than 286
Any example of int array please .
It happens when all candidates for pivots are close to either the maximum or the minimum value of the array.
java.util.DualPivotQuicksort#sort() chooses the pivots from 5 positions in the array:
int seventh = (length >> 3) + (length >> 6) + 1;
int e3 = (left + right) >>> 1; // The midpoint
int e2 = e3 - seventh;
int e1 = e2 - seventh;
int e4 = e3 + seventh;
int e5 = e4 + seventh;
So, in order to construct an array that satisfies the condition, we need to fill those 5 positions with extreme values. For example:
int[] x = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, /* e1 = 10 */
0, 0, 0, 0, 0, 0, -1, /* e2 = 17 */
0, 0, 0, 0, 0, 0, 0, /* e3 = 24 */
0, 0, 0, 0, 0, 0, 1, /* e4 = 31 */
0, 0, 0, 0, 0, 0, 2, /* e5 = 38 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
Arrays.sort(x);
And a non-trivial case where the method changes the boundaries of the central part before sorting it:
int[] x = {
70, 66, 11, 24, 10, 28, 58, 13, 19, 90, 15,
79, 16, 69, 39, 14, 10, 16,
40, 59, 47, 77, 90, 50, 50,
50, 16, 76, 86, 70, 33, 90,
24, 35, 73, 93, 87, 19, 91,
73, 87, 22, 15, 24, 92, 34, 35, 98, 11, 40
};

DC JS Limiting brushing height

Is there a way to limit the brushing height - say 50% of y axis (only from Y axis 0 - 250, brushing should work) ? Example fiddle
JS Code:
var hitslineChart = dc.barChart("#chart-line-hitsperday");
var data = [
{date: "12/27/2012", http_404: 2, http_200: 190, http_302: 100},
{date: "12/28/2012", http_404: 2, http_200: 10, http_302: 100},
{date: "12/29/2012", http_404: 1, http_200: 300, http_302: 200},
{date: "12/30/2012", http_404: 2, http_200: 90, http_302: 0},
{date: "12/31/2012", http_404: 2, http_200: 90, http_302: 0},
{date: "01/01/2013", http_404: 2, http_200: 90, http_302: 0},
{date: "01/02/2013", http_404: 1, http_200: 10, http_302: 1},
{date: "01/03/2013", http_404: 2, http_200: 90, http_302: 0},
{date: "01/04/2013", http_404: 2, http_200: 90, http_302: 0},
{date: "01/05/2013", http_404: 2, http_200: 90, http_302: 0},
{date: "01/06/2013", http_404: 2, http_200: 200, http_302: 1},
{date: "01/07/2013", http_404: 1, http_200: 200, http_302: 100}
];
var ndx = crossfilter(data);
var parseDate = d3.time.format("%m/%d/%Y").parse;
data.forEach(function(d) {
d.date = Date.parse(d.date);
d.total= d.http_404+d.http_200+d.http_302;
});
var dateDim = ndx.dimension(function(d) {return d.date;});
var hits = dateDim.group().reduceSum(function(d) {return d.total;});
var minDate = dateDim.bottom(1)[0].date;
var maxDate = dateDim.top(1)[0].date;
hitslineChart.width(500)
.height(200)
.dimension(dateDim)
.group(hits)
.x(d3.time.scale().domain([minDate,maxDate]));
dc.renderAll();
Thanks,
Arun
Although your example uses dc.js 1.7.0, I'm going to answer for dc.js 2.0, since it's a lot newer and a few APIs have changed.
The technique is to override the functions from the coordinateGridMixin which size the brush. This gets a little hairy, but it's possible.
It turns out we'll have to override three undocumented functions which render the brush, renderBrush, setBrushY, and (unfortunately) resizeHandlePath.
The reason this gets hairy is that we really want to override brushHeight, but that one is a private function.
We'll define our own like this:
function height_over_2() {
return (hitslineChart._xAxisY() - hitslineChart.margins().top)/2;
}
For renderBrush, we need to shift the brush down by height_over_2(). We'll pass through the call first, then modify the transform:
dc.override(hitslineChart, 'renderBrush', function(g) {
hitslineChart._renderBrush(g);
var gBrush = hitslineChart.select('g.brush')
.attr('transform', 'translate(' + hitslineChart.margins().left + ',' + (hitslineChart.margins().top + height_over_2()) + ')')
});
setBrushY we'll replace entirely (we could just assign to it, but we'll use dc.override for consistency):
dc.override(hitslineChart, 'setBrushY', function(gBrush) {
gBrush.selectAll('rect')
.attr('height', height_over_2());
gBrush.selectAll('.resize path')
.attr('d', hitslineChart.resizeHandlePath);
});
Finally, resizeHandlePath also uses the height, and here we (ugh) have to copy a big chunk of code out of dc.js, which was itself copied from the crossfilter demo:
dc.override(hitslineChart, 'resizeHandlePath', function (d) {
var e = +(d === 'e'), x = e ? 1 : -1, y = height_over_2() / 3;
return 'M' + (0.5 * x) + ',' + y +
'A6,6 0 0 ' + e + ' ' + (6.5 * x) + ',' + (y + 6) +
'V' + (2 * y - 6) +
'A6,6 0 0 ' + e + ' ' + (0.5 * x) + ',' + (2 * y) +
'Z' +
'M' + (2.5 * x) + ',' + (y + 8) +
'V' + (2 * y - 8) +
'M' + (4.5 * x) + ',' + (y + 8) +
'V' + (2 * y - 8);
});
Fork of your fiddle: http://jsfiddle.net/gordonwoodhull/anz9gfy0/13/

Drag the certain object on picbox doesn't drag the all image on it

I want to ask if it is possible to drag the image on picbox, when mouse cursor point to the certain object, so when I select the X-textbox, y-textbox, and select pin text
it will displat accourding my coordinate n pin number i selected.
But I want to drag them on picturebox. When mouse cursor touches that certain IC image, it can be drag. But not all the image on picbox drag together.
Can anyone give me some suggestion?
My full code below:
Option Explicit
Private mPic As Picture
Private mPicWidth As Single
Private mPicHeight As Single
Private mCurrentX As Single
Private mCurrentY As Single
Private mLeft As Single
Private mTop As Single
Private Sub Command5_Click()
Call draw_ic(Val(Text5), Val(Text6), Val(Text7))
End Sub
Private Sub Form_Load()
Picture1.AutoRedraw = True
Set mPic = Picture1.Image
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
mPicWidth = Me.ScaleX(mPic.Width, vbHimetric, Picture1.ScaleMode)
mPicHeight = Me.ScaleY(mPic.Height, vbHimetric, Picture1.ScaleMode)
ShowPictureAtPosition mLeft, mTop
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 0 Then
mCurrentX = X
mCurrentY = Y
ElseIf Button = vbLeftButton Then
ShowPictureAtPosition X + mLeft - mCurrentX, Y + mTop - mCurrentY
End If
End Sub
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
mLeft = X + mLeft - mCurrentX: mTop = Y + mTop - mCurrentY
End Sub
Private Sub ShowPictureAtPosition(pX As Single, pY As Single)
With Picture1
.Cls
.PaintPicture mPic, pX + 1, pY + 1, mPicWidth, mPicHeight
End With
End Sub
Function draw_ic(X, Y, pincount)
If pincount = 8 Then
Picture1.Line (X, Y)-(X + 120, Y + 48), vbBlack, B
Picture1.Line (X + 8, Y)-(X + 24, Y - 16), vbBlack, B
Picture1.Line (X + 34, Y)-(X + 50, Y - 16), vbBlack, B
Picture1.Line (X + 60, Y)-(X + 76, Y - 16), vbBlack, B
Picture1.Line (X + 86, Y)-(X + 102, Y - 16), vbBlack, B
Picture1.Line (X + 8, Y + 48)-(X + 24, Y + 64), vbBlack, B
Picture1.Line (X + 34, Y + 48)-(X + 50, Y + 64), vbBlack, B
Picture1.Line (X + 60, Y + 48)-(X + 76, Y + 64), vbBlack, B
Picture1.Line (X + 86, Y + 48)-(X + 102, Y + 64), vbBlack, B
ElseIf pincount = 12 Then
Picture1.Line (X, Y)-(X + 158, Y + 64), vbBlack, B
Picture1.Line (X + 8, Y)-(X + 16, Y - 8), vbBlack, B
Picture1.Line (X + 32, Y)-(X + 40, Y - 8), vbBlack, B
Picture1.Line (X + 56, Y)-(X + 64, Y - 8), vbBlack, B
Picture1.Line (X + 80, Y)-(X + 88, Y - 8), vbBlack, B
Picture1.Line (X + 104, Y)-(X + 112, Y - 8), vbBlack, B
Picture1.Line (X + 128, Y)-(X + 136, Y - 8), vbBlack, B
Picture1.Line (X + 8, Y + 64)-(X + 16, Y + 72), vbBlack, B
Picture1.Line (X + 32, Y + 64)-(X + 40, Y + 72), vbBlack, B
Picture1.Line (X + 56, Y + 64)-(X + 64, Y + 72), vbBlack, B
Picture1.Line (X + 80, Y + 64)-(X + 88, Y + 72), vbBlack, B
Picture1.Line (X + 104, Y + 64)-(X + 112, Y + 72), vbBlack, B
Picture1.Line (X + 128, Y + 64)-(X + 136, Y + 72), vbBlack, B
ElseIf pincount = 16 Then
Picture1.Line (X, Y)-(X + 222, Y + 72), vbBlack, B
Picture1.Line (X + 8, Y)-(X + 24, Y - 16), vbBlack, B
Picture1.Line (X + 34, Y)-(X + 50, Y - 16), vbBlack, B
Picture1.Line (X + 60, Y)-(X + 76, Y - 16), vbBlack, B
Picture1.Line (X + 86, Y)-(X + 102, Y - 16), vbBlack, B
Picture1.Line (X + 112, Y)-(X + 128, Y - 16), vbBlack, B
Picture1.Line (X + 138, Y)-(X + 154, Y - 16), vbBlack, B
Picture1.Line (X + 164, Y)-(X + 180, Y - 16), vbBlack, B
Picture1.Line (X + 190, Y)-(X + 206, Y - 16), vbBlack, B
Picture1.Line (X + 8, Y + 72)-(X + 24, Y + 88), vbBlack, B
Picture1.Line (X + 34, Y + 72)-(X + 50, Y + 88), vbBlack, B
Picture1.Line (X + 60, Y + 72)-(X + 76, Y + 88), vbBlack, B
Picture1.Line (X + 86, Y + 72)-(X + 102, Y + 88), vbBlack, B
Picture1.Line (X + 112, Y + 72)-(X + 128, Y + 88), vbBlack, B
Picture1.Line (X + 138, Y + 72)-(X + 154, Y + 88), vbBlack, B
Picture1.Line (X + 164, Y + 72)-(X + 180, Y + 88), vbBlack, B
Picture1.Line (X + 190, Y + 72)-(X + 206, Y + 88), vbBlack, B
End If
End Function
This is a very interesting project !
Consider the coordinates of the following image.
Every time that you create a new IC, if you save its top-left and bottom-right coordinates, it will help you to determine when the mouse pointer reaches its area by using the following code.
Private Function MouseCursorInsideRectangle(topLeftX As Integer, topLeftY As Integer, bottomRightX As Integer, bottomRightY As Integer, mouseX As Integer, mouseY As Integer) As Boolean
If mouseX >= topLeftX And mouseX <= bottomRightX And mouseY >= topLeftY And mouseY <= bottomRightY Then
MouseCursorInsideRectangle = True
Else
MouseCursorInsideRectangle = False
End If
End Function
This will resolve the issue of overlapping ICs, since you can check the coordinate of the IC that you want to draw and disallow the drawing, if it's over another one.
To solve the issue of moving only a certain IC and not the whole PictureBox image, you can clear the image when a drag event is in progress and draw all the ICs again (by using the coordinates that you saved during their creation) through iteration.
(Although it is not in your requirements, consider also saving the coordinates of the pins of each IC, so you can draw lines between ICs in the future).
EDIT
Try the below code
Option Explicit
Option Base 0
Private Type ICData
topLeftX As Integer
topLeftY As Integer
bottomRightX As Integer
bottomRightY As Integer
pinCount As Integer
End Type
Dim ICs() As ICData
Dim ICsIndex As Integer
Dim DraggedICIndex As Integer
Dim Xdifference As Integer
Dim Ydifference As Integer
Dim InitialX As Integer
Dim InitialY As Integer
Private Sub Form_Load()
ICsIndex = -1
DraggedICIndex = -1
Picture1.ScaleMode = 3
Picture1.AutoRedraw = True
End Sub
Private Sub Command5_Click()
Call save_ic(Val(text5), Val(text6), Val(text7))
Call draw_ics(-1)
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
DraggedICIndex = GetICIndex(CSng(X), CSng(Y))
If DraggedICIndex > -1 Then
InitialX = ICs(DraggedICIndex).topLeftX
InitialY = ICs(DraggedICIndex).topLeftY
Xdifference = Abs(X - ICs(DraggedICIndex).topLeftX)
Ydifference = Abs(Y - ICs(DraggedICIndex).topLeftY)
End If
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton And DraggedICIndex > -1 Then
Picture1.Cls
Call draw_ics(DraggedICIndex)
Call draw_ic(X - Xdifference, Y - Ydifference, ICs(DraggedICIndex).pinCount)
Dim ICWidth As Integer
Dim ICHeight As Integer
ICWidth = ICs(DraggedICIndex).bottomRightX - ICs(DraggedICIndex).topLeftX
ICHeight = ICs(DraggedICIndex).bottomRightY - ICs(DraggedICIndex).topLeftY
End If
End Sub
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If DraggedICIndex > -1 Then
Dim ICWidth As Integer
Dim ICHeight As Integer
ICWidth = ICs(DraggedICIndex).bottomRightX - ICs(DraggedICIndex).topLeftX
ICHeight = ICs(DraggedICIndex).bottomRightY - ICs(DraggedICIndex).topLeftY
ICs(DraggedICIndex).topLeftX = X - Xdifference
ICs(DraggedICIndex).topLeftY = Y - Ydifference
ICs(DraggedICIndex).bottomRightX = ICWidth + X - Xdifference
ICs(DraggedICIndex).bottomRightY = ICHeight + Y - Ydifference
End If
End Sub
Private Function MouseCursorInsideRectangle(location As ICData, mouseX As Integer, mouseY As Integer) As Boolean
If mouseX >= location.topLeftX And mouseX <= location.bottomRightX And mouseY >= location.topLeftY And mouseY <= location.bottomRightY Then
MouseCursorInsideRectangle = True
Else
MouseCursorInsideRectangle = False
End If
End Function
Private Function GetICIndex(mouseX As Integer, mouseY As Integer) As Integer
Dim i As Integer
For i = 0 To ICsIndex
If MouseCursorInsideRectangle(ICs(i), mouseX, mouseY) = True Then
GetICIndex = i
Exit Function
End If
Next
GetICIndex = -1
End Function
Sub save_ic(X, Y, pinCount)
If pinCount = 8 Then
ICsIndex = ICsIndex + 1
ReDim Preserve ICs(ICsIndex)
ICs(ICsIndex).topLeftX = X
ICs(ICsIndex).topLeftY = Y
ICs(ICsIndex).bottomRightX = X + 120
ICs(ICsIndex).bottomRightY = Y + 80
ICs(ICsIndex).pinCount = pinCount
ElseIf pinCount = 12 Then
ICsIndex = ICsIndex + 1
ReDim Preserve ICs(ICsIndex)
ICs(ICsIndex).topLeftX = X
ICs(ICsIndex).topLeftY = Y
ICs(ICsIndex).bottomRightX = X + 158
ICs(ICsIndex).bottomRightY = Y + 80
ICs(ICsIndex).pinCount = pinCount
ElseIf pinCount = 16 Then
ICsIndex = ICsIndex + 1
ReDim Preserve ICs(ICsIndex)
ICs(ICsIndex).topLeftX = X
ICs(ICsIndex).topLeftY = Y
ICs(ICsIndex).bottomRightX = X + 222
ICs(ICsIndex).bottomRightY = Y + 104
ICs(ICsIndex).pinCount = pinCount
End If
End Sub
Sub draw_ic(X, Y, pinCount)
If pinCount = 8 Then
Picture1.Line (X, Y + 16)-(X + 120, Y + 64), vbBlack, B
Picture1.Line (X + 8, Y + 16)-(X + 24, Y), vbBlack, B
Picture1.Line (X + 34, Y + 16)-(X + 50, Y), vbBlack, B
Picture1.Line (X + 60, Y + 16)-(X + 76, Y), vbBlack, B
Picture1.Line (X + 86, Y + 16)-(X + 102, Y), vbBlack, B
Picture1.Line (X + 8, Y + 64)-(X + 24, Y + 80), vbBlack, B
Picture1.Line (X + 34, Y + 64)-(X + 50, Y + 80), vbBlack, B
Picture1.Line (X + 60, Y + 64)-(X + 76, Y + 80), vbBlack, B
Picture1.Line (X + 86, Y + 64)-(X + 102, Y + 80), vbBlack, B
ElseIf pinCount = 12 Then
Picture1.Line (X, Y + 8)-(X + 158, Y + 72), vbBlack, B
Picture1.Line (X + 8, Y + 8)-(X + 16, Y), vbBlack, B
Picture1.Line (X + 32, Y + 8)-(X + 40, Y), vbBlack, B
Picture1.Line (X + 56, Y + 8)-(X + 64, Y), vbBlack, B
Picture1.Line (X + 80, Y + 8)-(X + 88, Y), vbBlack, B
Picture1.Line (X + 104, Y + 8)-(X + 112, Y), vbBlack, B
Picture1.Line (X + 128, Y + 8)-(X + 136, Y), vbBlack, B
Picture1.Line (X + 8, Y + 72)-(X + 16, Y + 80), vbBlack, B
Picture1.Line (X + 32, Y + 72)-(X + 40, Y + 80), vbBlack, B
Picture1.Line (X + 56, Y + 72)-(X + 64, Y + 80), vbBlack, B
Picture1.Line (X + 80, Y + 72)-(X + 88, Y + 80), vbBlack, B
Picture1.Line (X + 104, Y + 72)-(X + 112, Y + 80), vbBlack, B
Picture1.Line (X + 128, Y + 72)-(X + 136, Y + 80), vbBlack, B
ElseIf pinCount = 16 Then
Picture1.Line (X, Y + 16)-(X + 222, Y + 88), vbBlack, B
Picture1.Line (X + 8, Y + 16)-(X + 24, Y), vbBlack, B
Picture1.Line (X + 34, Y + 16)-(X + 50, Y), vbBlack, B
Picture1.Line (X + 60, Y + 16)-(X + 76, Y), vbBlack, B
Picture1.Line (X + 86, Y + 16)-(X + 102, Y), vbBlack, B
Picture1.Line (X + 112, Y + 16)-(X + 128, Y), vbBlack, B
Picture1.Line (X + 138, Y + 16)-(X + 154, Y), vbBlack, B
Picture1.Line (X + 164, Y + 16)-(X + 180, Y), vbBlack, B
Picture1.Line (X + 190, Y + 16)-(X + 206, Y), vbBlack, B
Picture1.Line (X + 8, Y + 88)-(X + 24, Y + 104), vbBlack, B
Picture1.Line (X + 34, Y + 88)-(X + 50, Y + 104), vbBlack, B
Picture1.Line (X + 60, Y + 88)-(X + 76, Y + 104), vbBlack, B
Picture1.Line (X + 86, Y + 88)-(X + 102, Y + 104), vbBlack, B
Picture1.Line (X + 112, Y + 88)-(X + 128, Y + 104), vbBlack, B
Picture1.Line (X + 138, Y + 88)-(X + 154, Y + 104), vbBlack, B
Picture1.Line (X + 164, Y + 88)-(X + 180, Y + 104), vbBlack, B
Picture1.Line (X + 190, Y + 88)-(X + 206, Y + 104), vbBlack, B
End If
End Sub
Sub draw_ics(exceptICIndex As Integer)
If ICsIndex > -1 Then
Dim i As Integer
For i = 0 To ICsIndex
If i <> exceptICIndex Then
Call draw_ic(ICs(i).topLeftX, ICs(i).topLeftY, ICs(i).pinCount)
End If
Next
End If
End Sub
I leave the IC overlap implementation on you :).

Display temperature as a color with C#?

Someone knows an algorithm that gets temperatue in Kelvin/Celsius and returns RGB?
Like in thermal cameras.
I found some links :
http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_T.html
http://www.fourmilab.ch/documents/specrend/specrend.c
But i cant figure what XYZ color is ?
I only have temperature in Celsius..
i can convert it to any temperature Temperature Conversion Formulas
UPDATE:
Blackbody Color Datafile
I have found this.. but those Kelvin degrees are impossible.. i mean
red suppose to be hot.. so why 8000k is blue and 1000k is red...
The best option is to use an image with GetPixel:
private void UpdateTemp()
{
Bitmap temps = (Bitmap)Properties.Resources.temp;
if (curTemp >= 0)
{
int i = curTemp;
if (i < 0)
i = 0;
if (i > temps.Width-1)
i = temps.Width-1;
this.BackColor = temps.GetPixel(i, 10);
}
}
Or building an array. Source
private static Color[] colors =
{
Color.FromArgb(155, 188, 255), // 40000
Color.FromArgb(155, 188, 255), // 39500
Color.FromArgb(155, 188, 255), // 39000
Color.FromArgb(155, 188, 255), // 38500
Color.FromArgb(156, 188, 255), // 38000
Color.FromArgb(156, 188, 255), // 37500
Color.FromArgb(156, 189, 255), // 37000
Color.FromArgb(156, 189, 255), // 36500
Color.FromArgb(156, 189, 255), // 36000
Color.FromArgb(157, 189, 255), // 35500
Color.FromArgb(157, 189, 255), // 35000
Color.FromArgb(157, 189, 255), // 34500
Color.FromArgb(157, 189, 255), // 34000
Color.FromArgb(157, 189, 255), // 33500
Color.FromArgb(158, 190, 255), // 33000
Color.FromArgb(158, 190, 255), // 32500
Color.FromArgb(158, 190, 255), // 32000
Color.FromArgb(158, 190, 255), // 31500
Color.FromArgb(159, 190, 255), // 31000
Color.FromArgb(159, 190, 255), // 30500
Color.FromArgb(159, 191, 255), // 30000
Color.FromArgb(159, 191, 255), // 29500
Color.FromArgb(160, 191, 255), // 29000
Color.FromArgb(160, 191, 255), // 28500
Color.FromArgb(160, 191, 255), // 28000
Color.FromArgb(161, 192, 255), // 27500
Color.FromArgb(161, 192, 255), // 27000
Color.FromArgb(161, 192, 255), // 26500
Color.FromArgb(162, 192, 255), // 26000
Color.FromArgb(162, 193, 255), // 25500
Color.FromArgb(163, 193, 255), // 25000
Color.FromArgb(163, 193, 255), // 24500
Color.FromArgb(163, 194, 255), // 24000
Color.FromArgb(164, 194, 255), // 23500
Color.FromArgb(164, 194, 255), // 23000
Color.FromArgb(165, 195, 255), // 22500
Color.FromArgb(166, 195, 255), // 22000
Color.FromArgb(166, 195, 255), // 21500
Color.FromArgb(167, 196, 255), // 21000
Color.FromArgb(168, 196, 255), // 20500
Color.FromArgb(168, 197, 255), // 20000
Color.FromArgb(169, 197, 255), // 19500
Color.FromArgb(170, 198, 255), // 19000
Color.FromArgb(171, 198, 255), // 18500
Color.FromArgb(172, 199, 255), // 18000
Color.FromArgb(173, 200, 255), // 17500
Color.FromArgb(174, 200, 255), // 17000
Color.FromArgb(175, 201, 255), // 16500
Color.FromArgb(176, 202, 255), // 16000
Color.FromArgb(177, 203, 255), // 15500
Color.FromArgb(179, 204, 255), // 15000
Color.FromArgb(180, 205, 255), // 14500
Color.FromArgb(182, 206, 255), // 14000
Color.FromArgb(184, 207, 255), // 13500
Color.FromArgb(186, 208, 255), // 13000
Color.FromArgb(188, 210, 255), // 12500
Color.FromArgb(191, 211, 255), // 12000
Color.FromArgb(193, 213, 255), // 11500
Color.FromArgb(196, 215, 255), // 11000
Color.FromArgb(200, 217, 255), // 10500
Color.FromArgb(204, 219, 255), // 10000
Color.FromArgb(208, 222, 255), // 9500
Color.FromArgb(214, 225, 255), // 9000
Color.FromArgb(220, 229, 255), // 8500
Color.FromArgb(227, 233, 255), // 8000
Color.FromArgb(235, 238, 255), // 7500
Color.FromArgb(245, 243, 255), // 7000
Color.FromArgb(255, 249, 253), // 6500
Color.FromArgb(255, 243, 239), // 6000
Color.FromArgb(255, 236, 224), // 5500
Color.FromArgb(255, 228, 206), // 5000
Color.FromArgb(255, 219, 186), // 4500
Color.FromArgb(255, 209, 163), // 4000
Color.FromArgb(255, 196, 137), // 3500
Color.FromArgb(255, 180, 107), // 3000
Color.FromArgb(255, 161, 72), // 2500
Color.FromArgb(255, 137, 18), // 2000
Color.FromArgb(255, 109, 0), // 1500
Color.FromArgb(255, 51, 0), // 1000
};
I realize this is a two-year old thread, but I had the same predicament.
I took the data from the color table and did applied piece-wise 5th order polynomial fitting using Numpy.polyfit in Python. From those coefficients I was able to come up with the C# function below. R-squared values for the fits are close to or exceed 0.999. It has less than .01% error through most of its domain, but it does have a couple of points where it is closer to 3%. Should be good enough for most situations though.
private Color blackBodyColor(double temp)
{
float x = (float)(temp / 1000.0);
float x2 = x * x;
float x3 = x2 * x;
float x4 = x3 * x;
float x5 = x4 * x;
float R, G, B = 0f;
// red
if (temp <= 6600)
R = 1f;
else
R = 0.0002889f * x5 - 0.01258f * x4 + 0.2148f * x3 - 1.776f * x2 + 6.907f * x - 8.723f;
// green
if (temp <= 6600)
G = -4.593e-05f * x5 + 0.001424f * x4 - 0.01489f * x3 + 0.0498f * x2 + 0.1669f * x - 0.1653f;
else
G = -1.308e-07f * x5 + 1.745e-05f * x4 - 0.0009116f * x3 + 0.02348f * x2 - 0.3048f * x + 2.159f;
// blue
if (temp <= 2000f)
B = 0f;
else if (temp < 6600f)
B = 1.764e-05f * x5 + 0.0003575f * x4 - 0.01554f * x3 + 0.1549f * x2 - 0.3682f * x + 0.2386f;
else
B = 1f;
return Color.FromScRgb(1f, R, G, B);
}
If get you right you are looking for a theoretical background on XYZ color space
Color temperature is based on the actual color of light emitted from something (theoretically, an "ideal black body") that emits light based solely on its temperature.
Some examples of this kind of light source: if you have an electric stove element that is glowing red, it might be around 1000K. A regular incandescent bulb filament is around 2700K, and the sun is roughly 5700K. All three are fair approximations of a "black body"; they emit a particular spectrum of light based on their actual temperature.
Many artificial light sources are not actually the "temperature" of the light they emit (and their spectra are generally not black-body spectra, either...). Instead, their "temperature" rating is the temperature a theoretical black body would have to be in order to emit light of that color. There are also colors that cannot be generated by a black body: light which is greenish or purplish compared to a more "natural"-looking black body illumination.
As mentioned in one of the comments, the kind of thermal camera displays you are probably thinking about are all false-color. In a false-color display, the colors are chosen for convenience only: so, for a thermal camera they might choose a "hot"-looking red for warm, and "cold"-looking blue for cold. But, they could just as easily choose a range from black to white, or fuschia to green.
Because false-color displays are arbitrary, you really need to check the color key to a particular image or system if you want to estimate the temperature (scientific images should generally have some kind of color key for this purpose). If you have no color key, and no documentation on how the image was generated, you are out of luck.
The above function overestimate red color when temp > 10000 K. Colors turn to purple when temp>14000. I refitted the data with 7th order polynomials. The coefficients of should be:
def temp_to_rgb(temp):
t = temp/1000.
# calculate red
if t < 6.527:
red = 1.0
else:
coeffs = [ 4.93596077e+00, -1.29917429e+00,
1.64810386e-01, -1.16449912e-02,
4.86540872e-04, -1.19453511e-05,
1.59255189e-07, -8.89357601e-10]
tt = min(t,40)
red = poly(coeffs,tt)
red = max(red,0)
red = min(red,1)
# calcuate green
if t < 0.85:
green = 0.0
elif t < 6.6:
coeffs = [ -4.95931720e-01, 1.08442658e+00,
-9.17444217e-01, 4.94501179e-01,
-1.48487675e-01, 2.49910386e-02,
-2.21528530e-03, 8.06118266e-05]
green = poly(coeffs,t)
else:
coeffs = [ 3.06119745e+00, -6.76337896e-01,
8.28276286e-02, -5.72828699e-03,
2.35931130e-04, -5.73391101e-06,
7.58711054e-08, -4.21266737e-10]
tt = min(t,40)
green = poly(coeffs,tt)
green = max(green,0)
green = min(green,1)
# calculate blue
if t < 1.9:
blue = 0.0
elif t < 6.6:
coeffs = [ 4.93997706e-01, -8.59349314e-01,
5.45514949e-01, -1.81694167e-01,
4.16704799e-02, -6.01602324e-03,
4.80731598e-04, -1.61366693e-05]
blue = poly(coeffs,t)
else:
blue = 1.0
blue = max(blue,0)
blue = min(blue,1)
return (red,green,blue)
Here poly(coeffs,x) = coeffs[0] + coeffs[1]*x + coeffs[2]*x**2 + ...
Sorry I am not familiar with C# but you can easily read the codes.
The error is within only 0.5% for most cases and at most 1.2% for red in temp = 6600 K. High order polynomials are adopted here so red and green must keep constant for temp > 40000 K, otherwise strange things will happen.

Resources